On 07/05/2011 10:46 AM, Martin Dunschen wrote:
> I sometimes use this construct:
> 
> answer = condition and value1 or value2
> 
> BUT there's a gotcha in here, think of 'condition' being True and value1
> = 0 !
> 
> So, having caught out a few times, I now just write it explicitely
> unless I am absolutely certain value1 can never calculate to False.

Yeah, in my case the values are usually string constants so that
wouldn't be a problem in itself but it is exactly the reason why it
keeps feeling shaky...

Best,

Flo



> On 05/07/2011 10:28, Flo Ledermann wrote:
>> Hi all,
>>
>> one of the very few things that still bugs me with Python is its lack of
>> the "?" operator as present in C and Java. The clarity and conciseness
>> of lines like
>>
>> answer = is_a ? "It's A!" : "Maybe B?"
>>
>> is just unmatched for me by its Python equivalents. It doesn't actually
>> really help that there are two commonly used alternatives in Python:
>>
>> answer = is_a and "It's A!" or "Maybe B?"
>>
>> or, alternatively since Python 2.5:
>>
>> answer = "It's A!" if is_a else "Maybe B?"
>>
>> Oh come on Guido, you could have done better than that!
>>
>> The first option is usually a surprise to non-Python people, and while
>> I've come to accept and even somewhat like it, the ambiguity of the
>> boolean operators involved always leaves me with the feeling of being a
>> bit non-waterproof, especially with a more complex expressions.
>>
>> The second option is more explicit, but swapping the order and putting
>> the (actually non-default) case before the comparison feels less
>> readable and a bit confusing, too.
>>
>> What do you  guys prefer? Did I overlook anything obvious? Has this been
>> discussed thousands of times?
>>
>> Best,
>>
>> Flo
>>
>> P.S. if you want to read up on some of the history of this issue, its
>> http://www.python.org/dev/peps/pep-0308/
>>
> 

-- 
To post: [email protected]
To unsubscribe: [email protected]
Feeds: http://groups.google.com/group/python-north-west/feeds
More options: http://groups.google.com/group/python-north-west

Reply via email to