Access to an "upper" instance of a class from another instance of a different class
- by BorrajaX
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!