UberJumper
UberJumper

Reputation: 21155

Weird Problem with Classes and Optional Arguments

Okay so this was driving me nuts all day.

Why does this happen:

class Foo:
    def __init__(self, bla = {}):
        self.task_defs = bla
    def __str__(self):
        return ''.join(str(self.task_defs))

a = Foo()
b = Foo()
a.task_defs['BAR'] = 1
print 'B is ==> %s' % str(b)
print 'A is ==> %s' % str(a)

Gives me the output:

B is ==> {'BAR': 1}
A is ==> {'BAR': 1}

I know it has to do with python passing everything by reference.

But why does this happen? This was literally making me go insane all day, basically causing me to tear my stuff apart. Shouldn't python be smart enough to deal with something like this?

Upvotes: 0

Views: 109

Answers (1)

Evan Fosmark
Evan Fosmark

Reputation: 101771

Since you have bla initially set to a mutable type (in this case a dict) in the arguments, it gets shared since bla doesn't get reinitialized to a new dict instance for each instance created for Foo. Here, try this instead:

class Foo:
    def __init__(self, bla=None):
        if bla is None:
            bla = {}
        self.task_defs = bla
    def __str__(self):
        return ''.join(str(self.task_defs))

a = Foo()
b = Foo()
a.task_defs['BAR'] = 1
print 'B is ==> %s' % str(b)
print 'A is ==> %s' % str(a)

Upvotes: 6

Related Questions