Is there a short-circuiting dictionary "get" method?

Steven Bethard steven.bethard at gmail.com
Wed Mar 9 15:22:09 EST 2005


Bill Mill wrote:
> On 9 Mar 2005 10:05:21 -0800, bearophileHUGS at lycos.com
> <bearophileHUGS at lycos.com> wrote:
> 
>>Maybe this can help:
>>
>>value = d.get('x', lambda: bigscaryfunction())
> 
> 
>>>>def test(): print 'gbye'
> ...
>>>>d = {}
>>>>z = d.get('x', lambda: test())
>>>>z
> <function <lambda> at 0x008D6870>
> 
> So this seems to be merely an obfuscation of:
> 
>>>>z = d.get('x', test)
>>>>z
> <function test at 0x008D66B0>
> 
> I just wanted to ask, am I missing something?

Nope that looks right.  See "Overuse of lambda" in 
http://www.python.org/moin/DubiousPython for discussion of exactly this 
mistake.

STeVe



More information about the Python-list mailing list