| Author |
Message |
Casey Hawthorne *nix forums beginner
Joined: 12 May 2005
Posts: 26
|
Posted: Fri Jul 21, 2006 3:41 am Post subject:
Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
Since there was talk of if-then-else not being allowed in lambda
expressions, the following is from "Dive into Python"
The and-or conditional expression trick from page 41 of "Dive into
Python"
Wrap the arguments in lists and then take the first element.
| Quote: | a = ""
b = "second"
(1 and [a] or [b])[0]
'' |
--
Regards,
Casey |
|
| Back to top |
|
 |
Peter Otten *nix forums Guru
Joined: 20 Feb 2005
Posts: 464
|
Posted: Fri Jul 21, 2006 5:48 am Post subject:
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
Casey Hawthorne wrote:
| Quote: | Since there was talk of if-then-else not being allowed in lambda
expressions, the following is from "Dive into Python"
The and-or conditional expression trick from page 41 of "Dive into
Python"
Wrap the arguments in lists and then take the first element.
a = ""
b = "second"
(1 and [a] or [b])[0]
''
|
# python 2.5
| Quote: | a, b = "", 0
a if False else b
0
a if True else b
'' |
Time to tear out that page. Really.
Peter |
|
| Back to top |
|
 |
antroy@gmail.com *nix forums addict
Joined: 24 May 2006
Posts: 52
|
Posted: Fri Jul 21, 2006 10:04 am Post subject:
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
| Quote: | # python 2.5
a, b = "", 0
a if False else b
0
a if True else b
''
Time to tear out that page. Really.
|
Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade. |
|
| Back to top |
|
 |
Peter Otten *nix forums Guru
Joined: 20 Feb 2005
Posts: 464
|
Posted: Fri Jul 21, 2006 12:08 pm Post subject:
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
Ant wrote:
| Quote: |
# python 2.5
a, b = "", 0
a if False else b
0
a if True else b
''
Time to tear out that page. Really.
Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade.
|
Ok, use
if cond:
value = ... # expression that must not be evaluated unless it absolutely
# has to be
else:
value = ... # same thing
which will withstand the test of time. Now tear out that page...
Peter |
|
| Back to top |
|
 |
bruno modulix *nix forums Guru
Joined: 21 Feb 2005
Posts: 819
|
Posted: Fri Jul 21, 2006 12:13 pm Post subject:
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
Peter Otten wrote:
| Quote: | Ant wrote:
# python 2.5
a, b = "", 0
a if False else b
0
a if True else b
''
Time to tear out that page. Really.
Not quite - 2.5 hasn't been released in its final version yet, and many
projects I should imagine will take a while to upgrade.
Ok, use
if cond:
value = ... # expression that must not be evaluated unless it absolutely
# has to be
else:
value = ... # same thing
which will withstand the test of time.
|
But isn't allowed in a lambda !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb@xiludom.gro'.split('@')])" |
|
| Back to top |
|
 |
Peter Otten *nix forums Guru
Joined: 20 Feb 2005
Posts: 464
|
Posted: Fri Jul 21, 2006 12:36 pm Post subject:
Re: Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"
|
|
|
Bruno Desthuilliers wrote:
| Quote: | But isn't allowed in a lambda !-)
|
Then tear out that lambada page, too, I was tempted to say, but I will
desist. For now...
Peter |
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|