
December 14th, 2005, 08:15 PM
| | | ?: in Python
How can do elegantly in Python:
if condition:
a=1
else:
a=2
like in C:
a=condition?1:2 | 
December 14th, 2005, 08:35 PM
| | | 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" | 
December 14th, 2005, 08:55 PM
| | | 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. | 
December 14th, 2005, 09:15 PM
| | | 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 | 
December 14th, 2005, 09:15 PM
| | | Re: ?: in Python | 
December 14th, 2005, 09:25 PM
| | | 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 | 
December 14th, 2005, 10:05 PM
| | | 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. | 
December 14th, 2005, 10:15 PM
| | | 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. | 
December 15th, 2005, 02:25 AM
| | | 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. | 
December 15th, 2005, 02:45 AM
| | | 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 | 
December 15th, 2005, 04:15 PM
| | | 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. | 
December 15th, 2005, 11:25 PM
| | | 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. | 
December 16th, 2005, 02:35 AM
| | | 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 | 
December 16th, 2005, 04:25 AM
| | | 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 | 
December 18th, 2005, 03:55 AM
| | | 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. |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | 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.
|