checking if a list is empty
Laurent Claessens
moky.math at gmail.com
Wed May 11 05:48:16 EDT 2011
> In this case, the interpretation of an arbitrary object as a boolean
> is peculiar for python. An empty list is a real, existing object, and
> the supposition that [] be false is counter-intuitive. It can be
> learnt, and the shorthand may be powerful when it is, but it will
> confuse many readers.
Once I wrote something like:
def f(x=None):
if x:
print x
else:
print "I have no value"
The caller of that function was something like f(cos(2*theta)) where
theta come from some computations.
Well. When it turned out that theta was equal to pi/4, I got "I have no
value". I spent a while to figure out the problem :)
Conclusion: the boolean value of an object is to be used with care in
order to tests if an optional parameter is given or not (when default
value is None).
Have a good noon
Laurent
More information about the Python-list
mailing list