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
  accessing local variable outside class priyanka08 3 127 Sep-24-2019, 10:00 AM
Last Post: buran
  How to serialize custom class objects in JSON? Exsul1 4 176 Sep-23-2019, 08:27 AM
Last Post: wavic
  Python complains that class instance is not defined colt 3 162 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  How do I write class objects to a file in binary mode? Exsul1 7 410 Sep-14-2019, 09:33 PM
Last Post: snippsat
  My class has name error message 357mag 3 126 Sep-04-2019, 03:29 PM
Last Post: snippsat
  Appending a list in a class from a callback function snizbatch 5 185 Sep-01-2019, 06:27 AM
Last Post: snizbatch
  class returns NoneType Object istemihan 0 121 Aug-12-2019, 11:47 AM
Last Post: istemihan
  class random var write to array storzo 5 219 Aug-02-2019, 03:26 PM
Last Post: storzo
  Object and type class Uchikago 2 177 Jul-28-2019, 10:35 AM
Last Post: DeaD_EyE
  how to add class instance attributes from list 999masks 2 187 Jul-22-2019, 07:59 AM
Last Post: 999masks

Forum Jump:


Users browsing this thread: 1 Guest(s)