Movatterモバイル変換


[0]ホーム

URL:


Python bug with [] as default value in functions?

Duncan Boothduncan at rcp.co.uk
Thu Apr 12 08:53:48 EDT 2001


[posted and mailed]Dinu Gherman <gherman at darwin.in-berlin.de> wrote in <3AD5989D.E22E2CBF at darwin.in-berlin.de>:> I noticed a very strange behaviour: I've got a recursive func-> tion like this:>>   foo(spam, eggs=[]):>     ...>     return foo(spam, eggs=eggs)>     # the following is the same:>     # return foo(spam, eggs)>> where the default argument for eggs, the empty list, is *empty*> for the top-level call *only* when it is explicitly provided> with the function call like this:>>   bar = foo(mySpam, eggs=[])>This is documented behaviour. See the FAQ athttp://www.python.org/doc/FAQ.html#6.25 for a description.In general if you want a mutable object to be used as a default argument (and the function needs to modify the object), use None instead and create the object inside the function.


More information about the Python-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp