473,714 Members | 3,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1567
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
1983
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 twice and hate the syntax, and I would be delighted if I could use lambda expressions instead when Orcas is released. *** Sent via Developersdex http://www.developersdex.com ***
5
4434
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() is called or if Python compile and generate a new function object every time. If it is the latter, will there be a huge performance hit? Would someone give some hint about how exactly Python does this internally? def fn_outer(v):
41
3045
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 http://www.embedded.com/2000/0005/0005feat2.htm ,
1
1328
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 hoping this entry really helps those who are struggling with this topic. -- Gregory A. Beamer MVP, MCP: +I, SE, SD, DBA
20
3991
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 like SQL and those 'high level' languages that try to be too clever by half and pack a lot of syntax in as short a space as possible. Like Egyptian hieroglyphics, like Chinese, like those non-alphabet languages, it's just way too much information...
0
1228
by: Pavel Minaev | last post by:
On Aug 14, 1:37 pm, 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 only to be able to maintain other people's code. Also, every time I hear such sentiments - "generics/closures/type inference/... are hard to understand and compromise readability" (there were plenty of such as new major language features were...
3
2098
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 not recognized). I am using the 'standard' using directives using System;using System.Collections.Generic;using System.Diagnostics;using System.Text; And Visual Studio 2007.
6
2304
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
8712
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9316
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9077
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7954
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5959
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2525
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2113
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.