Python Forum
two different objects, but somehow the values are shared between them
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
two different objects, but somehow the values are shared between them
#1
As the title said, I have two different objects from two classes that share the same inheritance.
When I modify an inherited variable in obj1, it also modifies the variable in obj2.
Can someone please help me and tell how do I change the value only on obj1?

For further understanding of the issue, imagine that I have the following class:

class fish:
    position = [0,0,0];
    def setPosition(self,pos):
        self.position = pos;

class tuna(fish):
    #extra variables here!

class salmon(fish):
    #extra variables here!


obj1 = salmon();
obj2 = tuna();
obj1.setPosition([1,1,1]);
When I check obj2.position, it is the same as obj1.
Reply
#2
Salmon inherits fish so includes all "fish" variables. The purpose of this is so you change one variable and all of the classes can use the change. To isolate the fish variable you create a separate instance instead of inheriting.
class Fish():
    def __init__(self):
        self.position = [0,0,0];

class Tuna():
    def __init__(self):
        #extra variables here!
        self.this_fish=Fish()

class Salmon():
    def __init__(self):
        #extra variables here!
        self.this_fish=Fish()


obj1 = Salmon()
obj2 = Tuna()
obj1.this_fish.position = [1,1,1]
print(obj1.this_fish.position)
print(obj2.this_fish.position) 
Reply
#3
Thank you, woooee. I will try that. Although I dont get why the values "propagate" through the objects. Im used to C (C# and C++), and when you set a value of an object there it doesnt propagate to other objects of the same class.
Reply
#4
It doesn't "propagate". You are sending the same object to both Salmon and Tuna, i.e. a class object, not a class instance object https://www.toptal.com/python/python-cla...ough-guide
Reply
#5
I'm a little bit confused, with the code you wrote here (I edited it a little bit):
class Fish:
    position = [0, 0, 0]

    def set_position(self, pos):
        self.position = pos


class Tuna(Fish):
    #extra variables here!


class Salmon(Fish):
    #extra variables here!


obj1 = Salmon()
obj2 = Tuna()
obj1.set_position([1, 1, 1])
both obj1 and obj2 will have their own 'position' and
obj1.set_position([1, 1, 1])
will change position value only for obj1 'position', and if you check obj2.position after this line you can see it's still [0,0,0]
Reply
#6
Obviously you didn't read the link posted previously about the difference between class and instance attributes. Creating a new instance attribute named self.position does not change the class attribute, Fish.position. Try this and see what the results are
obj1.set_position("this is a new variable") 
I won't be answering any more as this concept seems to be beyond your understanding.
Reply
#7
(Apr-19-2018, 08:09 PM)RaphaelMG Wrote: Im used to C (C# and C++)

Ok, so what you're doing, in terms of those other languages, is having a static variable position that's unbound from any instance. In python, they're called class attributes/methods instead of static, but it's essentially the same thing. In order to have instance variables, they'd need to be defined in the __init__ method, not outside of there.

Here's some example code to try to show the difference:
>>> class Spam:
...   # class attribute, shared by all instances
...   things = []
...   def add(self, thing):
...     self.things.append(thing)
...     return self.things
...
>>> eggs = Spam()
>>> eggs.add("first")
['first']
>>> ham = Spam()
>>> ham.add("second")
['first', 'second']
>>> Spam.things
['first', 'second']
>>>
>>> class Bike:
...   def __init__(self):
...     self.things = []
...   def add(self, thing):
...     self.things.append(thing)
...     return self.things
...
>>> two_wheels = Bike()
>>> two_wheels.add("first")
['first']
>>> four_wheels = Bike()
>>> four_wheels.add("second")
['second']
>>> Bike.things
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Bike' has no attribute 'things'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Shared reference of variables... Denial 1 1,374 Aug-29-2020, 01:52 PM
Last Post: snippsat
  How to install and use a shared libary, via a .dll? ninjaisfast 0 1,271 Jul-09-2020, 03:23 PM
Last Post: ninjaisfast
  Divisors shared the second numbers mircea_dragu 1 2,015 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  running just one process shared among uses Skaperen 3 2,925 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Shared reference and equality zyo 3 3,096 Jun-30-2018, 07:10 PM
Last Post: ljmetzger
  Shared queues l00p1n6 3 2,948 May-15-2018, 01:38 PM
Last Post: DeaD_EyE
  updating values in objects mercator 2 3,012 Dec-07-2017, 07:41 PM
Last Post: mercator

Forum Jump:

User Panel Messages

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