tags:

views:

65

answers:

1

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?

+6  A: 

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)
Evan Fosmark
Yeah i know but that becomes very messy when dealing with constructors that require multiple optional arguments.I don't know i am just more or less frustrated that i wasted so much time on something like this. :(
UberJumper
It's nothing about multiple optional arguments. The problem is that you are using a mutable default, in this case the dictionary.
Lennart Regebro
No but if i have multiple optional arguments, lets say 5 optional arguments, that take lists/maps, then this becomes a fairly big pain. I think python should just fix it :)
UberJumper