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


Messages In This Thread
Instances sharing attributes - by midarq - Sep-19-2019, 04:26 PM
RE: Instances sharing attributes - by ichabod801 - Sep-19-2019, 07:18 PM
RE: Instances sharing attributes - by midarq - Sep-20-2019, 10:30 AM
RE: Instances sharing attributes - by perfringo - Sep-20-2019, 10:45 AM
RE: Instances sharing attributes - by midarq - Sep-20-2019, 11:13 AM

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