Why are default values sometimes shared between objects?
It is often expected that a function CALL creates new objects for default values. This is not what happens. Default values are created when the function is DEFINED, that is, there is only one such object that all functions refer to. If that object is changed, subsequent calls to the function will refer to this changed object. By definition, immutable objects (like numbers, strings, tuples, None) are safe from change. Changes to mutable objects (like dictionaries, lists, class instances) is what causes the confusion. Because of this feature it is good programming practice not to use mutable objects as default values, but to introduce them in the function. Don’t write: def foo(dict={}): # XXX shared reference to one dict for all calls … but: def foo(dict=None): if dict is None: dict = {} # create a new dict for local namespace See page 182 of “Internet Programming with Python” for one discussion of this feature. Or see the top of page 144 or bottom of page 277 in “Programming Python” f