Python Forum
Instances sharing attributes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Instances sharing attributes
#1
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
Reply
#2
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
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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
Reply
#4
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
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 []
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best way to secure API key when sharing quarinteen 2 290 Jan-19-2024, 04:46 PM
Last Post: deanhystad
Question [solved] Classes, assign an attributes to a class not to instances.. SpongeB0B 4 891 May-20-2023, 04:08 PM
Last Post: SpongeB0B
  How to install modules for 2.7 (or sharing from 3.8)) Persisto 2 2,374 Dec-31-2021, 02:33 PM
Last Post: Persisto
  multiprocessing and sharing object cyrduf 0 2,012 Feb-02-2021, 08:16 PM
Last Post: cyrduf
  sharing variables between two processes Kiyoshi767 1 1,848 Nov-07-2020, 04:00 AM
Last Post: ndc85430
  Sharing my code emirasal 2 1,998 Oct-04-2020, 02:21 PM
Last Post: emirasal
  Sharing X Axis in Sub plots JoeDainton123 1 2,142 Aug-22-2020, 04:11 AM
Last Post: deanhystad
  Instances as attributes sapra90 2 2,297 Jun-26-2019, 04:38 AM
Last Post: woooee
  Sharing variables across modules j.crater 4 3,408 Jul-30-2018, 09:09 PM
Last Post: j.crater
  Class Instances overriding class members and attributes. Leaf 7 6,860 Nov-29-2017, 06:08 AM
Last Post: Leaf

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020