Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 11th, 2004, 07:57 AM
Wizard2003's Avatar
Wizard2003 Wizard2003 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 206 Wizard2003 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 h 29 m 24 sec
Reputation Power: 6
lambda for two statements

Hi all,

I have a dictionary like the following:
Code:
bla = {float: lambda a, b: a.asString(b),          # case 1
         int:    lambda a, b, c: a.asString(b,c)    # case 2
        }


But now in the first case I want to add something like:
Code:
a.round()

The return value of that statement is Null.

Is it possible to integrate that statement somehow in the lambda function?

Regards,

Wizard2003

Reply With Quote
  #2  
Old August 11th, 2004, 04:18 PM
DevCoach DevCoach is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2004
Location: London, England
Posts: 1,203 DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level)DevCoach User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 5 Days 17 h 10 m 35 sec
Reputation Power: 262
You could do

Code:
lambda a, b: a.round() or a.asString(b)


Since a.round() always returns Null, the or operator will go on and execute the second part of the statement.

IMHO this is not very readable though, since the purpose of the or is not obvious.

Another way would be to put the result of each call into an array or tuple and then return the last one:

Code:
lambda a, b: ( a.round(), a.asString(b) )[-1]


Again this is not very readable, but has the advantage that it will still work if the first function returns a True value. It be made more readable by wrapping it up in a function:

Code:

def do_each(*vals): return vals[-1]

lambda a, b: do_each( a.round(), a.asString(b) )


The disadvantage of the last two is that the order of evaluation is not guaranteed by the language - a.asString(b) could be evaluated before a.round(). In the current implementation it is always evaluated left to right as you would expect, but that could change in future implementations.

Dave - The Developers' Coach

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > lambda for two statements


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway