pitfall for your amusement
Mel Wilson
mwilson at the-wire.com
Tue Nov 12 17:31:14 EST 2002
In article <aqrka3$1emk$1 at nntp6.u.washington.edu>,
"Russell E. Owen" <owen at nospam.invalid> wrote:
> [ ... ]
>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.
I'm not finding that. Am I misunderstanding something? See below ..
Regards. Mel.
Python 2.1.3 (#35, Apr 8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>> def f2(dflt=[]):
... return dflt
...
>>> f2()
[]
>>> f2(3)
3
>>> f2()
[]
>>> f2([1,2])
[1, 2]
>>> f2()
[]
>>>
More information about the Python-list
mailing list