?: in Python

A

Andy Leszczynski

How can do elegantly in Python:

if condition:
a=1
else:
a=2

like in C:

a=condition?1:2
 
A

Andy Leszczynski

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]:print a
2

Like it too, thx. Switch does not have to be bool, so it is more
powerfull than ?:.

A.
 
L

Lawrence Oluyede

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]:print a
2
 
B

Bengt Richter

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]:print a
2
You won't like it in a case like
a = (2**20**20, 2)[switch]
or
a = (m/n, sys.maxint)[n==0]
the point is that if/else only evaluates
the expression in one branch, as with C ternary.

You're right, there is a PEP and a ternary expression
coming to python though.

Regards,
Bengt Richter
 
P

Peter Hansen

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]:print a
2

Like it too, thx. Switch does not have to be bool, so it is more
powerfull than ?:.

Actually, if "switch" is neither bool nor a number that equals 0 or 1,
you'll have trouble... using bool(switch) instead would do what you
thought that did, I guess.

-Peter
 
S

Steven D'Aprano

How can do elegantly in Python:

if condition:
a=1
else:
a=2

like in C:

a=condition?1:2

I thought you wanted to do it *elegantly*?

Your first solution is perfectly elegant to my eyes, unlike that horrible
C syntax.
 
B

bonono

Andy said:
How can do elegantly in Python:

if condition:
a=1
else:
a=2

like in C:

a=condition?1:2

a=(condition and [1] or [2])[0]

For this simple snippet, I don't think it is better than if/else, But
you can use it in map/reduce or list comprehension/generator expression.
 
A

Andy Leszczynski

Steven said:
I thought you wanted to do it *elegantly*?

Your first solution is perfectly elegant to my eyes, unlike that horrible
C syntax.

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.

A.
 
D

Dave Hansen

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]:print a
2

Note, however, you have the logic backwards. To duplicate the
functionality of the OP's example, you need

a = (2,1)[condition]

or

a = (1,2)[not condition]

Regards,
-=Dave
 
S

Steven D'Aprano

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.

"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.
 
A

Andrew Koenig

Andy Leszczynski said:
How can do elegantly in Python:

if condition:
a=1
else:
a=2

I believe that before long Python will support

a=1 if condition else 2
 
K

Kent Johnson

Andy said:
How can do elegantly in Python:

if condition:
a=1
else:
a=2

like in C:

a=condition?1:2

a = condition and A or B
is concise but will fail if A can evaluate as false, e.g.
a = condition and None or 2 # won't do what you want

I tend to use 'condition and A or B' if I'm sure A won't be false, otherwise just write
out the if / else.

Kent
 
A

Andy Leszczynski

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.

First:
"Special cases aren't special enough to break the rules.
Although practicality beats purity."


Second, let's look at again:
>if condition:
> a=1
>else:
> a=2

The primer meaning behind that is that I want to assign something to a.
What I want to assign is secondary issue. I do not like C syntax of ?:
either but I think it is just practical and self-explanatory.

A.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,056
Latest member
ElidaMarlo

Latest Threads

Top