Python Forum
Get variable from class inside another class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get variable from class inside another class
#1
Hi everyone,

I am building a code roughly organised as the example here:

class parent:
    
    def __init__(self):
        self.variable_1 = 1
        self.instance_child = []
        
    class child:
        
        def __init(self):
            self.variable_2 = 0
            
        def get_variable_1(self):
            self.variable_2 = 1 ###
            print(self.variable_1)
            
    def new_child(self):
        self.instance_child.append(self.child())
    # --------------------
            
instance = parent()
instance.new_child()
instance.instance_child[0].get_variable_1()
I have one class "parent" that contains another class "child", and instances are creates sequencially in the list "instance_child". What I am trying to do is to read variable_1 from inside the class 'child' without copying it. I could of course do " self.instance_child[0].variable_2 = self.variable_1 ", but I am trying to get a value from "parent" without repeating it in "child".

Is there any solution to do so ?
Thanks :)

PS: I can also accept an alternative way of writting this code, as long as I can call multiple instances of 'child' into 'parent'.
Reply
#2
You could do this by having the child keep track of the parent:

class parent:
     
    def __init__(self):
        self.variable_1 = 1
        self.instance_child = []
         
    class child:
         
        def __init(self, parent):
            self.parent = parent
            self.variable_2 = 0
             
        def get_variable_1(self):
            self.variable_2 = 1 ###
            print(self.parent.variable_1)
             
    def new_child(self):
        self.instance_child.append(self.child(self))
    # --------------------
             
instance = parent()
instance.new_child()
instance.instance_child[0].get_variable_1()
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Simple and efficient, thanks a lot.
One technical question if I may: Would you know whether all the information variable in 'parent' are repeated into 'child' in memory, or is it literally taking track of the content ?
Reply
#4
The information is not being repeated. The parent attribute of the child is just pointing to where the parent is stored in memory.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  class and runtime akbarza 4 212 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 393 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 526 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 580 Feb-04-2024, 09:35 AM
Last Post: Gribouillis
  Class test : good way to split methods into several files paul18fr 4 380 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  Good class design - with a Snake game as an example bear 1 1,703 Jan-24-2024, 08:36 AM
Last Post: annakenna
  question about __repr__ in a class akbarza 4 505 Jan-12-2024, 11:22 AM
Last Post: DeaD_EyE
  Variable definitions inside loop / could be better? gugarciap 2 364 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  error in class: TypeError: 'str' object is not callable akbarza 2 418 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  super() in class akbarza 1 386 Dec-19-2023, 12:55 PM
Last Post: menator01

Forum Jump:

User Panel Messages

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