pitfall for your amusement
Russell E. Owen
owen at nospam.invalid
Tue Nov 12 14:19:21 EST 2002
In my own learning of Python I found a fair number of warnings about the
problem you mentioned, so I feel it is pretty well documented. On the
other hand, it was a familiar problem from Smalltalk and I may just have
gotten lucky in my reading.
If you are collecting pitfalls, here's my least favorite: "never use a
mutable object as a default value". A classic example:
def badfunc(alist=[]):
...
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 []
...
-- Russell
More information about the Python-list
mailing list