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
  Accessing method attributes of python class Abedin 6 717 Apr-14-2025, 07:02 AM
Last Post: buran
  Python class members based on a type value voidtrance 7 1,104 Apr-11-2025, 10:10 PM
Last Post: deanhystad
  Create a new subclass in a Python extension based on an existing class voidtrance 6 1,301 Mar-25-2025, 06:37 PM
Last Post: voidtrance
  not able to call the variable inside the if/elif function mareeswaran 3 538 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  printing/out put issue with class arabuamir 3 947 Aug-25-2024, 09:29 AM
Last Post: arabuamir
  Class test : good way to split methods into several files paul18fr 5 3,630 Jul-17-2024, 11:12 AM
Last Post: felixandrea
  Variable being erased inside of if statement deusablutum 8 2,059 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  [split] Class and methods ebn852_pan 15 3,243 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 1,277 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 1,077 May-13-2024, 04:04 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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