Dennis Lee Bieber
2/20/2008 7:25:00 PM
On Wed, 20 Feb 2008 11:04:26 -0800 (PST), nikipore@googlemail.com
declaimed the following in comp.lang.python:
I guess it /has/ been about two weeks since this bit the last
person...
> def __init__(self, x={}):
def __init__(self, x=None):
if x:
self.data = x
else:
self.data = {}
Python Reference Manual, section 7.5, Function Definitions
"""
....
Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same ``pre-computed'' value is used
for each call. This is especially important to understand when a default
parameter is a mutable object, such as a list or a dictionary: if the
function modifies the object (e.g. by appending an item to a list), the
default value is in effect modified. This is generally not what was
intended. A way around this is to use None as the default, and
explicitly test for it in the body of the function, e.g.:
....
"""
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/