Non-intuitive treatment of empty list as default parameter value on class method 
Author Message
 Non-intuitive treatment of empty list as default parameter value on class method

If on a class method you define a default parameter value equal to an empty
list, and then start to create instances of the class using constructor
calls with default values (i.e. no parameter value given), you get the
following IMHO non-intuitive behaviour (Python 2.2 /  IDLE was used):

Quote:
>>> class C:

 def __init__(self, aList=[]):
  self.someList = aList

Quote:
>>> c1=C()   # create an instance of C with default parameters
>>> print c1.someList
[]
>>> c1.someList.append('spam')
>>> print c1.someList
['spam']
>>> c2=C()
>>> print c2.someList
['spam']
>>> print id(c1.someList), id(c2.someList)     # someList in c1 and c2 point

to the same list object
13244816 13244816

Apparently the empty list is created by reference in the class' namespace
and being mutable the reference persists after the c1.someList.append call
and the second (or subsequent) class instantiation call. A quick way to get
the intended behaviour is by explicit parameter assignment:

Quote:
>>> c1=C(aList=[])
>>> print c1.someList
[]
>>> c1.someList.append('spam')
>>> print c1.someList
['spam']
>>> c2=C(aList=[])
>>> print c2.someList
[]
>>> print id(c1.someList), id(c2.someList)

13438160 13484848

I sense some inconsistency here because apparently in the global namespace
different empty list instances are created.

With an adapted class definition again the intended behaviour can be
achieved:

Quote:
>>> class C:

 def __init__(self, aList=None):
  if aList is None:
   self.someList = []
  else:
   self.someList = aList

Quote:
>>> c1 = C()
>>> c1.someList.append('spam')
>>> print c1.someList
['spam']
>>> c2 = C()
>>> print c2.someList
[]
>>> print id(c1.someList), id(c2.someList)

13656800 13681120

Is the behaviour as illustrated in the first excerpt as intended? Can
somebody enlighten me?

Best regards, HPdK



Fri, 09 Sep 2005 19:21:47 GMT  
 Non-intuitive treatment of empty list as default parameter value on class method

Quote:
> Is the behaviour as illustrated in the first excerpt as intended? Can
> somebody enlighten me?

Yes:

http://www.python.org/cgi-bin/faqw.py?req=show&file=faq06.025.htp

Cheers,
M.

--
    FORD:  Just pust the fish in your ear, come on, it's only a
           little one.
  ARTHUR:  Uuuuuuuuggh!
                    -- The Hitch-Hikers Guide to the Galaxy, Episode 1



Fri, 09 Sep 2005 19:28:24 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. extending ruby: defining method with default parameters value...

2. Extracting method parameter and default-values?

3. default (empty) method in [incr tcl]

4. find non-empty value in a dictionary

5. Turning a list value in a parameter into a list for eval without quasiquote/unquote

6. Treatment of class variables in inherited classes

7. Default values for named formal parameters

8. Default value on signal parameter of a procedure

9. default values of function parameters (construtors)

10. Why default parameter value not working?

11. Mutable default values for function parameters

12. proc default values for parameters

 

 
Powered by phpBB® Forum Software