pitfall for your amusement
Erik Max Francis
max at alcyone.com
Tue Nov 12 16:30:02 EST 2002
"Russell E. Owen" wrote:
> The default value of alist will not stay [] (an empty list) but
> instead
> is affected by whatever you pass in for "alist". Very tricky and
> unpleasant.
>
> Here's my usual solution:
> def okfunc(alist=None):
> alist = alist or []
If you're using None as a sentinel value, it's probably better to test
for None-ness (via alist is None) rather than test for truth, since many
things can be false which are not None.
It's unlikely that in this particular example it would pose a problem,
but in the more general case it could. The idiom _I_ use is:
def f(l=None):
if l is None:
l = []
...
--
Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
__ San Jose, CA, USA / 37 20 N 121 53 W / &tSftDotIotE
/ \ If a thing is worth doing, then it is worth doing badly.
\__/ G.K. Chesterton
ZOE / http://www.alcyone.com/pyos/zoe/
A simple Python OpenGL rendering engine.
More information about the Python-list
mailing list