+2  A: 

From: http://www.network-theory.co.uk/docs/pytut/DefaultArgumentValues.html

The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes. For example, the following function accumulates the arguments passed to it on subsequent calls:

def f(a, L=[]):
    L.append(a)
    return L

print f(1)
print f(2)
print f(3)

This will print

[1]
[1, 2]
[1, 2, 3]

If you don't want the default to be shared between subsequent calls, you can write the function like this instead:

def f(a, L=None):
    if L is None:
        L = []
    L.append(a)
    return L
spoon16
Technically the default value is evaluated each time the function definition is executed. For example, if you define a function within a function, and give the inner function a default argument, that default argument gets evaluated each time the outer function is called. This can be used to get the same sort of behaviour provided by C `static` variables, though I'm not sure if doing so is considered good style; in most or all cases it would probably be better to use a normal closure.
intuited
are you sure you're legally allowed to copy other peoples' content?
SilentGhost
It's an excerpt of a book which was published as part of a python tutorial. I properly sourced the source I got it from and they properly source the source they got it from... I don't think it's a problem. If any moderators disagree though my feelings will not be hurt if this answer is removed.
spoon16
there should really be a rule that you can't provide non-cw to answers that you vote to close on.
aaronasterling
sorry, I voted to close after I answered which was before I realized there was an exact duplicate
spoon16