0

Are there any circumstances where a default keyword argument might get set implicitly?

For example, suppose we have a class:

class A(object):
  def __init__(self,keys=[],children=[]):
    print "A(keys=%s,children=%s)"%(keys,children)
    self.keys=keys
    self.children=children

Is it ever possible for there to be a different between:

A(keys=[],children=[v])

and

A(children=[v])

?

I have a program where when leaving keys implicit, they are getting set to a value other than the default empty list.

Chris Merck
  • 454
  • 3
  • 12
  • 1
    `I have a program where when leaving keys implicit, they are getting set to a value other than the default empty list. `. Can you please show that program? – thefourtheye Sep 19 '14 at 13:58
  • Ah, indeed this *is* due to the mutable default argument. How counterintuitive! I worked around the problem by making copies of the input parameters before assigning to member variables. So `self.keys=keys` becomes `self.keys=list(keys)`, and similar for `children`. – Chris Merck Sep 19 '14 at 16:01

0 Answers0