This is an old copy of the Python FAQ. The information here may be outdated.

Why are default values shared between objects?

Default values are created by the def statement, not when the function is called. Consider this function:

def foo(D={}):  # Danger: shared reference to one dict for all calls
    ... compute something ...
    D[key] = value
    return D

The first time you call this function, D contains a single item. The second time, D contains two items because when foo() begins executing, D starts out with an item already in it.

It is often expected that a function call creates new objects for default values. This is not what happens. Default values are created exactly once, when the function is defined (by executing the def statement). If that object is changed, like the dictionary in this example, subsequent calls to the function will refer to this changed object.

By definition, immutable objects such as numbers, strings, tuples, and None, are safe from change. Changes to mutable objects such as dictionaries, lists, and class instances can lead to confusion.

Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use None as the default value and inside the function, check if the parameter is None and create a new list/dictionary/whatever if it is. For example, don’t write:

def foo(dict={}):


def foo(dict=None):
    if dict is None:
        dict = {} # create a new dict for local namespace

This feature can be useful. When you have a function that’s time-consuming to compute, a common technique is to cache the parameters and the resulting value of each call to the function, and return the cached value if the same value is requested again. This is called memoizing, and can be implemented like this:

# Callers will never provide a third parameter for this function.
def expensive(arg1, arg2, _cache={}):
    if _cache.has_key((arg1, arg2)):
        return _cache[(arg1, arg2)]

    # Calculate the value
    result = ... expensive computation ...
    _cache[(arg1, arg2)] = result           # Store result in the cache
    return result

You could use a global variable containing a dictionary instead of the default value; it’s a matter of taste.

You can also use default arguments to bind local variables to objects rather than names. The first loop in the following example creates a new callback for each slot, but all callbacks end up with reference to the outer variable slot. In the second loop, explicit object binding is used to bind to the current value instead:

for slot in range(10):
    def callback():
        print "called from slot", slot      # This always prints "9"
    register_callback(slot, callback)

for slot in range(10):
    def callback(slot=slot):
        print "called from slot", slot
    register_callback(slot, callback)       # This does the right thing

CATEGORY: general


A Django site. rendered by a django application. hosted by webfaction.