Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I have strange behavior with the following :-
def set_diary_time_data(day_str, x={}):
# Dict for template in html
print 'Set diary time data'
print x
I call this function with :-
x = heading_queries.set_diary_time_data(day_str)
So the dictionary x is not passed as a parameter so it should be initialized to {}
However on repeated calls to this function the values placed in x are retained from the earlier call. This is in django and is called when a user logs in, the details of the previous user are retained in x. (x is not set as a global variable.)
I can overcome this by changing the function to
def set_diary_time_data(day_str, x=None):
if not x: x = {}
(Python 2.7)