class constructors: class vs. instance defaults

Steven Bethard steven.bethard at gmail.com
Thu Oct 7 13:53:34 EDT 2004


Steve Holden <steve <at> holdenweb.com> writes:
> 
> Of course that means that
> 
> 	(foo_d is None) or {}
> 
> works, giving you the best of both worlds.

I don't think this does what the OP wants it to:

>>> def f(x=None):
...     return (x is not None) or {}
...
>>> f()
{}
>>> f({})
True

Note that (x is not None) evaluates to True, not x.  Note that you can't solve 
this by introducing an 'and' either because {} evaluates to False in a boolean 
context:

>>> def f(x=None):
...     return x is not None and x or {}
...
>>> f()
{}
>>> d = {}
>>> f(d)
{}
>>> f(d) is d
False


(Another) Steve




More information about the Python-list mailing list