On 5 July 2011 10:28, Flo Ledermann <[email protected]> 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!
>>
> What do you guys prefer? Did I overlook anything obvious? Has this been
> discussed thousands of times?
It probably has been discussed many many times. Probably a good
subject for a flame war.
Because I'm something of caveman, I very rarely use either form, and
usually recommend not using ternary operator (I still have code that's
supported on Python 2.3 and kinda supported on Python 2.2).
There's also the (insanely obscure) indexing into a list trick:
answer = ['no','yes'][is_it_done]
Which I have to admit a guilty fondness for when selecting array sizes:
array.array('BH'[bits > 8], sometastysequence]
drj
--
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