0

The Python documentation says about keyword arguments (glossary):

...The variable name designates the local name in the function to which the value is assigned...

Thus I thought different instances of a class are truly different unless stated explicitly. Yet the following code refers one member variable of both instances of the class to the /same/ object, apparently via the given default value (as if the keyword dictionary of the method was a class variable ("static")). Once one modifies the member everything's fine, but if for some reason that doesn't happen:

  1. I wonder if there is a good reason for the code to behave like it does, because it's not what I would expect at all (see markings in code and output) and
  2. Whether there is a good solution to the problem (apart from using 'copy'). Thanks.

    class C:
        def setParams(self):
            self.e = []
        def setParamsKw(self, kw=['default list']): # <----- kw arg.
            self.eKw = kw                           # <--- that member should
                                                    #      default to the
                                                    #      default argument
                                                    #      (use 'kw.copy()'?)
    
    def outputMember():
        print " c1.e=", c1.e
        print " c2.e=", c2.e
        print " type(c1.e)=", type(c1.e)
        print " type(c2.e)=", type(c2.e)
        print " c1.e == c2.e:", c1.e == c2.e
        print " c1.e is c2.e:", c1.e is c2.e
    
    def outputMemberKw():
        print " c1.eKw=", c1.eKw
        print " c2.eKw=", c2.eKw
        print " type(c1.eKw)=", type(c1.eKw)
        print " type(c2.eKw)=", type(c2.eKw)
        print " c1.eKw == c2.eKw:", c1.eKw == c2.eKw
        print " c1.eKw is c2.eKw:", c1.eKw is c2.eKw # <----- this result
                                                     #      is unexpected
    
    
    c1 = C()
    c2 = C()
    
    print " c1 == c2:",  c1 == c2
    print " c1 is c2:",  c1 is c2
    
    print "Calling setParams for both instances:"
    c1.setParams()
    c2.setParams()
    outputMember()
    
    print "Calling setParamsKw for both instances:"
    c1.setParamsKw()
    c2.setParamsKw()
    outputMemberKw()
    
    print "Now manually modifying members of c1:"
    c1.e = [1, 2, 3, 4, 5]
    c1.eKw = [1, 2, 3, 4, 5]
    print "e:"
    outputMember()
    print "eKw:"
    outputMemberKw()
    

Output:

c1 == c2: False
 c1 is c2: False
Calling setParams for both instances:
 c1.e= []
 c2.e= []
 type(c1.e)= <type 'list'>
 type(c2.e)= <type 'list'>
 c1.e == c2.e: True
 c1.e is c2.e: False
Calling setParamsKw for both instances:
 c1.eKw= ['default list']
 c2.eKw= ['default list']
 type(c1.eKw)= <type 'list'>
 type(c2.eKw)= <type 'list'>
 c1.eKw == c2.eKw: True
 c1.eKw is c2.eKw: True
Now manually modifying members of c1:
e:
 c1.e= [1, 2, 3, 4, 5]
 c2.e= []
 type(c1.e)= <type 'list'>
 type(c2.e)= <type 'list'>
 c1.e == c2.e: False
 c1.e is c2.e: False
eKw:
 c1.eKw= [1, 2, 3, 4, 5]
 c2.eKw= ['default list']
 type(c1.eKw)= <type 'list'>
 type(c2.eKw)= <type 'list'>
 c1.eKw == c2.eKw: False
 c1.eKw is c2.eKw: False
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
Heinz
  • 1

1 Answers1

0
def setParamsKw(self, kw=None): # <----- kw arg.
            self.eKw = kw  if kw == None else ['default list']

should work

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179