Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"

Reply
Thread Tools

Since there was talk of if-then-else not being allowed in lambda expressions, the following is from "Dive into Python"

 
 
Casey Hawthorne
Guest
Posts: n/a
 
      07-21-2006
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]

''

--
Regards,
Casey
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      07-21-2006
Casey Hawthorne wrote:

> 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
>>> a, b = "", 0
>>> a if False else b

0
>>> a if True else b

''

Time to tear out that page. Really.

Peter
 
Reply With Quote
 
 
 
 
Ant
Guest
Posts: n/a
 
      07-21-2006

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

 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-21-2006
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. Now tear out that page...

Peter
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      07-21-2006
Peter Otten wrote:
> 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 '(E-Mail Removed)'.split('@')])"
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      07-21-2006
Bruno Desthuilliers wrote:

> 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

 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      07-21-2006
On Fri, 21 Jul 2006 14:36:30 +0200, Peter Otten <(E-Mail Removed)>
declaimed the following in comp.lang.python:

> Then tear out that lambada page, too, I was tempted to say, but I will
> desist. For now...
>

If Python is now doing the lambada, I don't think I want to watch...
<G>
--
Wulfraed Dennis Lee Bieber KD6MOG
http://www.velocityreviews.com/forums/(E-Mail Removed) (E-Mail Removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (E-Mail Removed))
HTTP://www.bestiaria.com/
 
Reply With Quote
 
 
 
Reply

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Type of lambda function returning a lambda function... Haochen Xie C++ 4 03-17-2013 11:23 PM
Why defining a constant in a method is not allowed but usingself.class.const_set is allowed? IƱaki Baz Castillo Ruby 13 05-01-2011 06:09 PM
lambda vs non-lambda proc Steve Dogers Ruby 1 03-30-2009 10:11 PM
Why is lambda allowed as a key in a dict? Daniel Fetchinson Python 4 03-10-2009 11:34 AM
Re: Lambda as declarative idiom (was RE: what is lambda used for inreal code?) Roman Suzi Python 13 01-07-2005 09:33 PM



Advertisments