2

Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument

can anyone explain me this?

class Strange(object):
    def mutate(self, x=[]):
        x.append(1)
        return x

obj = Strange()
print obj.mutate()
another_obj = Strange()
print another_obj.mutate() 

>> [1]
>> [1, 1] 

mutate() is called without the optional parameter. shouldn't x then always be []? what confuses me also is that the value of x is also shared across different objects...

Community
  • 1
  • 1

0 Answers0