<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4134.100" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2><FONT face="Times New Roman" size=3>The default 
expression "{}" is evaluated only once, when the function<BR>is defined.  
The value of the expression is stored as a property of the<BR>function 
object.  Thus the binding for r doesn't change, but it's value<BR>is a 
dictionary, which is mutable.<BR><BR>The moral of the story is: mutable default 
arguments are dangerous.<BR>Actually, mutable arguments in general are 
dangerous, unless you<BR>know how they work.<BR><BR>What you need to do is 
this:<BR><BR>>>> def test(r={}):<BR>...     r = 
r.copy()<BR>...     r[time.time()] = 
time.time()<BR>...     return r<BR>...    
<BR>>>> test()<BR>{997013843.75999999: 
997013843.75999999}<BR>>>> test()<BR>{997013845.57000005: 
997013845.57000005}<BR>>>> test()<BR>{997013846.88999999: 
997013846.88999999}<BR><BR>Note that the copy also protects your dictionary 
argument from <BR>side effects if one is passed in.  In the original 
version you get:<BR><BR>>>> def 
test(r={}):<BR>...     r[time.time()] = 
time.time()<BR>...     return r<BR>... <BR>>>> z = 
{'a':1}<BR>>>> test(z)<BR>{997014161.34000003: 997014161.34000003, 'a': 
1}<BR>>>> z<BR>{997014161.34000003: 997014161.34000003, 'a': 
1}<BR><BR>This may or may not be what you want, but "functional 
programming"<BR>monks consider such things to be sacrilegious.  In fact, 
your example<BR>should give functional programming monks a good reason to go 
find a<BR>different language!  :-)<BR><BR>- Ken Seehof<BR></FONT><A 
href="mailto:kseehof@neuralintegrator.com"><FONT face="Times New Roman" 
size=3>kseehof@neuralintegrator.com</FONT></A><BR><A 
href="http://www.neuralintegrator.com/kseehof"><FONT face="Times New Roman" 
size=3>www.neuralintegrator.com/kseehof</FONT></A><BR><BR><FONT 
face="Times New Roman" size=3>From: "Morten W. Petersen" <</FONT><A 
href="mailto:morten@thingamy.net"><FONT face="Times New Roman" 
size=3>morten@thingamy.net</FONT></A><FONT face="Times New Roman" 
size=3>><BR>> Hi,<BR>> <BR>> after trying to set an empty dictionary 
as a default keyword arguments'<BR>> value, this happened (same thing 
happened on 2.0.1 BTW):<BR>> <BR>> </FONT><A 
href="mailto:morten@debian:~$"><FONT face="Times New Roman" 
size=3>morten@debian:~$</FONT></A><FONT face="Times New Roman" size=3> 
python<BR>> Python 1.5.2 (#0, Apr 10 2001, 10:03:44)  [GCC 2.95.3 
20010219<BR>> (prerelease)] on linux2 Copyright 1991-1995 Stichting 
Mathematisch<BR>> Centrum, Amsterdam<BR>> >>> import time<BR>> 
>>> def test(r={}):<BR>> ...     r[time.time()] 
= time.time()<BR>> ...     return r<BR>> ...<BR>> 
>>> test()<BR>> {997015577.922: 997015577.922}<BR>> >>> 
test()<BR>> {997015578.849: 997015578.849, 997015577.922: 
997015577.922}<BR>> >>> test()<BR>> {997015579.446: 
997015579.446, 997015578.849: 997015578.849, <BR>>  997015577.922: 
997015577.922<BR>> <BR>> I would assume that r would be re-initialized on 
every call, but that's<BR>> not happening;  could anyone explain this 
behaviour?<BR>> <BR>> Thanks,<BR>> <BR>> Morten<BR>> <BR>> -- 
<BR>> </FONT><A 
href="http://mail.python.org/mailman/listinfo/python-list"><FONT 
face="Times New Roman" 
size=3>http://mail.python.org/mailman/listinfo/python-list</FONT></A><BR></FONT></DIV></BODY></HTML>