multipart-message nevyn bengtsson's blog

featured articles 🦄, about, archive, tags

The truth, it hurts, it hurts! (Default parameters in Python seem to be evaluated once)

class Bar(object):
  pass
 
class Foo(object):
  def __init__(self, a = Bar()):
    super(Foo, self).__init__()
    self.a = a
 
foo1 = Foo()
foo2 = Foo()
 
print repr(foo1) + repr(foo2)
print repr(foo1.a) + repr(foo2.a)
# foo1.a and foo2.a is the same instance

Tagged python, coding