Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 14th, 2005, 08:15 PM
Andy Leszczynski
Guest
 
Posts: n/a
Default ?: in Python

How can do elegantly in Python:

if condition:
a=1
else:
a=2

like in C:

a=condition?1:2

  #2  
Old December 14th, 2005, 08:35 PM
Lawrence Oluyede
Guest
 
Posts: n/a
Default Re: ?: in Python

Il 2005-12-14, Andy Leszczynski <yahoo@nospam.leszczynscy> ha scritto:[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2
>[/color]

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


--
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"
  #3  
Old December 14th, 2005, 08:55 PM
Andy Leszczynski
Guest
 
Posts: n/a
Default Re: ?: in Python

Lawrence Oluyede wrote:[color=blue]
> Il 2005-12-14, Andy Leszczynski <yahoo@nospam.leszczynscy> ha scritto:
>[color=green]
>>How can do elegantly in Python:
>>
>>if condition:
>> a=1
>>else:
>> a=2
>>
>>like in C:
>>
>>a=condition?1:2
>>[/color]
>
>
> 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
>
>[/color]

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

A.
  #4  
Old December 14th, 2005, 09:15 PM
Bengt Richter
Guest
 
Posts: n/a
Default Re: ?: in Python

On Wed, 14 Dec 2005 21:16:23 +0100, Lawrence Oluyede <raims@dot.com> wrote:
[color=blue]
>Il 2005-12-14, Andy Leszczynski <yahoo@nospam.leszczynscy> ha scritto:[color=green]
>> How can do elegantly in Python:
>>
>> if condition:
>> a=1
>> else:
>> a=2
>>
>> like in C:
>>
>> a=condition?1:2
>>[/color]
>
>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
>[/color]
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
  #5  
Old December 14th, 2005, 09:15 PM
gene tani
Guest
 
Posts: n/a
Default Re: ?: in Python


Andy Leszczynski wrote:[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2[/color]

google for ternary operator

http://www.python.org/doc/faq/progra...rnary-operator
http://www.norvig.com/python-iaq.html

http://mail.python.org/pipermail/pyt...er/310854.html
http://www.python.org/peps/pep-0308.html

http://diveintopython.org/power_of_i...r.html#d0e9975
http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52310

  #6  
Old December 14th, 2005, 09:25 PM
Peter Hansen
Guest
 
Posts: n/a
Default Re: ?: in Python

Andy Leszczynski wrote:[color=blue]
> Lawrence Oluyede wrote:[color=green]
>>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[/color]
>
> Like it too, thx. Switch does not have to be bool, so it is more
> powerfull than ?:.[/color]

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

  #7  
Old December 14th, 2005, 10:05 PM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: ?: in Python

On Wed, 14 Dec 2005 14:09:10 -0500, Andy Leszczynski wrote:
[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2[/color]

I thought you wanted to do it *elegantly*?

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



--
Steven.

  #8  
Old December 14th, 2005, 10:15 PM
bonono@gmail.com
Guest
 
Posts: n/a
Default Re: ?: in Python


Andy Leszczynski wrote:[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2[/color]

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.

  #9  
Old December 15th, 2005, 02:25 AM
Andy Leszczynski
Guest
 
Posts: n/a
Default Re: ?: in Python

Steven D'Aprano wrote:[color=blue]
> On Wed, 14 Dec 2005 14:09:10 -0500, Andy Leszczynski wrote:
>
>[color=green]
>>How can do elegantly in Python:
>>
>>if condition:
>> a=1
>>else:
>> a=2
>>
>>like in C:
>>
>>a=condition?1:2[/color]
>
>
> I thought you wanted to do it *elegantly*?
>
> Your first solution is perfectly elegant to my eyes, unlike that horrible
> C syntax.[/color]

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.
  #10  
Old December 15th, 2005, 02:45 AM
Steven Bethard
Guest
 
Posts: n/a
Default Re: ?: in Python

Andy Leszczynski wrote:[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2[/color]

Step (1): Wait for Python 2.5[1]
Step (2): Write the code::

a = 1 if condition else 2

STeVe

[1]http://www.python.org/peps/pep-0308.html
  #11  
Old December 15th, 2005, 04:15 PM
Dave Hansen
Guest
 
Posts: n/a
Default Re: ?: in Python

On Wed, 14 Dec 2005 21:16:23 +0100 in comp.lang.python, Lawrence
Oluyede <raims@dot.com> wrote:
[color=blue]
>Il 2005-12-14, Andy Leszczynski <yahoo@nospam.leszczynscy> ha scritto:[color=green]
>> How can do elegantly in Python:
>>
>> if condition:
>> a=1
>> else:
>> a=2
>>
>> like in C:
>>
>> a=condition?1:2
>>[/color]
>
>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[/color]

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

--
Change is inevitable, progress is not.
  #12  
Old December 15th, 2005, 11:25 PM
Steven D'Aprano
Guest
 
Posts: n/a
Default Re: ?: in Python

On Wed, 14 Dec 2005 20:17:28 -0500, Andy Leszczynski wrote:
[color=blue]
> 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.[/color]

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


--
Steven.

  #13  
Old December 16th, 2005, 02:35 AM
Andrew Koenig
Guest
 
Posts: n/a
Default Re: in Python

"Andy Leszczynski" <yahoo@nospam.leszczynscy> wrote in message
news:paudnWJmxOHh5j3enZ2dnUVZ_tadnZ2d@comcast.com. ..[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2[/color]

I believe that before long Python will support

a=1 if condition else 2



  #14  
Old December 16th, 2005, 04:25 AM
Kent Johnson
Guest
 
Posts: n/a
Default Re: ?: in Python

Andy Leszczynski wrote:[color=blue]
> How can do elegantly in Python:
>
> if condition:
> a=1
> else:
> a=2
>
> like in C:
>
> a=condition?1:2
>[/color]

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
  #15  
Old December 18th, 2005, 03:55 AM
Andy Leszczynski
Guest
 
Posts: n/a
Default Re: ?: in Python

Steven D'Aprano wrote:[color=blue]
> On Wed, 14 Dec 2005 20:17:28 -0500, Andy Leszczynski wrote:
>
>[color=green]
>>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.[/color]
>
>
> "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.
>
>[/color]

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


Second, let's look at again:[color=blue]
>if condition:
> a=1
>else:
> a=2[/color]

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.
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles