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
#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


Messages In This Thread
RE: two different objects, but somehow the values are shared between them - by woooee - Apr-19-2018, 07:53 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Shared reference of variables... Denial 1 1,457 Aug-29-2020, 01:52 PM
Last Post: snippsat
  How to install and use a shared libary, via a .dll? ninjaisfast 0 1,323 Jul-09-2020, 03:23 PM
Last Post: ninjaisfast
  Divisors shared the second numbers mircea_dragu 1 2,078 Feb-07-2019, 10:09 PM
Last Post: ichabod801
  running just one process shared among uses Skaperen 3 3,038 Aug-07-2018, 12:12 AM
Last Post: Skaperen
  Shared reference and equality zyo 3 3,227 Jun-30-2018, 07:10 PM
Last Post: ljmetzger
  Shared queues l00p1n6 3 3,036 May-15-2018, 01:38 PM
Last Post: DeaD_EyE
  updating values in objects mercator 2 3,088 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