A
Andy Leszczynski
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Lawrence said:Il 2005-12-14 said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There's also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]rint a
2
Il 2005-12-14 said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Andy said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
You won't like it in a case likeIl 2005-12-14 said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There's also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]rint a
2
Andy said:Lawrence said:There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There's also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]rint a
2
Like it too, thx. Switch does not have to be bool, so it is more
powerfull than ?:.
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Andy said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Steven said:I thought you wanted to do it *elegantly*?
Your first solution is perfectly elegant to my eyes, unlike that horrible
C syntax.
Andy said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Il 2005-12-14 said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
There are tons of threads on this newsgroup and in the python-dev mailing
list about a ternary operator. There's also a PEP AFAIK.
I like this:
In [1]:switch = True
In [2]:a = (1, 2)[switch]
In [3]rint a
2
I can tell you what is not elegant in the if else: approach. It is
logically a one operation while you are forced to use varaible "a"
twice. Fundamental flaw IMO.
Andy Leszczynski said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
Andy said:How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2
Steven said:"Logically" one operation?
def twenty_countries_in_seven_days_bus_tour():
...
if today() == Monday:
write_postcode_to_mother("We must be in Belgium.")
else:
get_back_on_the_bus("Not again!")
...
if...else expressions with a single operation are just a special case.
Perhaps a common special case, but still a special case.
>if condition:
> a=1
>else:
> a=2
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.