Python Forum

Full Version: Instances sharing attributes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I have the following problem:
Why is the instance B updating and how do i prevent that?
class myclass():

    def __init__(self, x=[]):
        self.x = x

    def append_to(self):
        self.x.append('foo')
A=myclass()
B=myclass()
A.append_to()
B.x
This returns ['foo'].

Thank you for your help
You are using a list as a default. Defaults are evaluated once and stored, not evaluated each time you call the method. Since lists are mutable, self.x becomes a pointer to that list, not a copy of that list. That makes every instance share the same self.x, unless x is passed explicitly. You generally solve this with a default of None:

class myclass():
 
    def __init__(self, x=None):
        if x is None:
            self.x = []
        else:
            self.x = x
Thank you for your solution. I wonder however if there is a more elegant way of achieving the same results. My code so far looks like this:
class my_data():
    def __init__(self, mn=None, r=None, x=None, y=None):
        if x is None:
            self.x=[]
        else:
            self.x = x
        if y is None:
            self.y=[]
        else:
            self.y = y
        if mn is None:
            self.mn=[]
        else:
            self.mn = mn
        if r is None:
            self.r=[]
        else:
            self.r= r
Thank you in advance
You can take advantage of Python 'returning operand' feature:

>>> scale = None
>>> scale = scale or None
>>> print(scale)
None
>>> scale = 10
>>> scale = scale or None
>>> scale
10
This is much more compact! Thanks!

class my_data():    
    def __init__(self, mn=None, r=None, x=None, y=None):
        self.x  = x or []
        self.y  = y or []
        self.mn = mn or []
        self.r  = r or []