473,394 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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.
>>a = ""
b = "second"
(1 and [a] or [b])[0]
''

--
Regards,
Casey
Jul 21 '06 #1
5 1519
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
Jul 21 '06 #2
Ant
# 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.

Jul 21 '06 #3
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
Jul 21 '06 #4
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 'o****@xiludom.gro'.split('@')])"
Jul 21 '06 #5
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

Jul 21 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mike P | last post by:
I've just been reading up on lambda expressions and how you can use them to make selections from XML in a similar syntax to SQL. Does this mean no more need for Xpath? I have used Xpath once or...
5
by: beginner | last post by:
Hi All, It is really convenient to use nested functions and lambda expressions. What I'd like to know is if Python compiles fn_inner() only once and change the binding of v every time fn_outer()...
41
by: dspfun | last post by:
Hi! Are suffixes allowed on expressions? For example, is the following allowed: #define SECONDS_PER_YEAR (60 * 60 * 365) UL I found this in a C test at...
1
by: Cowboy \(Gregory A. Beamer\) | last post by:
If you have a tough time getting your head around Lambda Expressions, see if this blog entry helps: http://tinyurl.com/4yeckd I would gladly welcome comments, critiques and questions, as I am...
20
by: raylopez99 | last post by:
Took a look at all the fuss about "lambda expressions" from Jon Skeet's excellent book "C# in Depth". Jon has an example, reproduced below (excerpt) on lambda expressions. My n00b take: it's...
0
by: Pavel Minaev | last post by:
On Aug 14, 1:37pm, raylopez99 <raylope...@yahoo.comwrote: You are, of course, entitled to your opinion, but lambdas are a part of C# 3.0, and any C# developer has to know their ins and outs if...
3
by: raylopez99 | last post by:
The headline says it all. Great minds think alike: read the blog below from three years ago, as endorsed by Ritchie, who coinvented C. BTW the below lambda expression code will not work (.Where...
6
by: Stefan Hoffmann | last post by:
hi @all, I stumbled upon this one: MethodInvoker listMethodInvoker = () =list = Broker.RetrieveList<T>(); This works fine for me under VS2008 and target .Net 2.0. The question is: Why?
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.