Bottom Page

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'.
Quote
#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

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

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  split by character class Skaperen 3 152 Today, 02:29 AM
Last Post: Skaperen
  Class takes no arguments Myang123 2 146 Jul-10-2019, 11:18 PM
Last Post: Myang123
  I carnt see publish/subscribe messages from the class I created. sdf1444 0 64 Jul-10-2019, 02:21 PM
Last Post: sdf1444
  Return a value when I equal an object from a class ihouses 4 152 Jul-10-2019, 02:44 AM
Last Post: SheeppOSU
  Conditional importing inside a function Roger 1 134 Jul-03-2019, 10:02 PM
Last Post: Gribouillis
  Embedding or adding IDE like "repl" inside Flask app sray 1 77 Jul-03-2019, 03:13 PM
Last Post: nilamo
  Find a given file recursively inside a directory alinaveed786 1 135 Jul-01-2019, 01:53 PM
Last Post: ichabod801
  Have two class instances affect one another The_Zookinator 3 152 Jun-26-2019, 06:56 PM
Last Post: The_Zookinator
  How to save a class object to a file? SheeppOSU 2 113 Jun-22-2019, 11:54 PM
Last Post: metulburr
  spread same class into separate files in python asheru93 3 158 Jun-19-2019, 02:06 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)