Access to an "upper" instance of a class from another instance of a different class

Posted by BorrajaX on Stack Overflow See other posts from Stack Overflow or by BorrajaX
Published on 2010-06-16T16:18:49Z Indexed on 2010/06/16 16:22 UTC
Read the original article Hit count: 173

Filed under:
|
|

Hello everyone!

I have a tricky question and probably what I want to do is not even possible but... who knows... Python seems very flexible and powerful...

I'd like to know if there's a way to access to the class (or its fields) where an object is instanciated. Let's say I have:

def Class1:
    def __init__(self):
        self.title = "randomTitle"
        self.anotherField = float()
        self.class2Field = Class2()

and the class whose type will be the class2Field:

def Class2:
    def __init__(self):
        self.field1 = ""
        self.field2 = ""
        # . . . #

I'd like to know if there's a way to access the instance of Class1 from the instance of Class2 that is declared in Class1 (meaning, accessing the fields of Class1 from the variable self.class2Field in that Class1 instance)

I know I can always change the init in Class2 to accept a Class1 parameter, but I'd like to know if there's another way of "climbing" through the class hierachy...

Thank you very much!

© Stack Overflow or respective owner

Related posts about python

Related posts about oop