tags:

views:

54

answers:

3

Hello everyone!

I'd like to know if there's a way to access to the class (or its fields) where an object is instantiated. 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 hierarchy.

Thank you very much!

+4  A: 

Yes, but don't do it. Pass the object explicitly.

Ignacio Vazquez-Abrams
:D Come ooooon :D I just need to access one of the fields to show proper debug messages... Is not gonna hurt anyone... :D :DThx for the fast reply, though...
BorrajaX
+1  A: 

There's no "class hierarchy" to speak of, here. Your Class1 objects contain Class2 objects, but your Class2 objects don't know that.

You could define self.parent in Class2 to be the instance of Class1 that refers to the object in self.class2Field, and have your Class1 instance passed in by __init__:

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

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

It's ugly, it's more overhead, and it requires that Class2 always be contained (at least the way that I wrote it above), but it might do what you want.

eksortso
Yeah... I guess I might have to wrap it... but it's a pity :(It makes things way more complicated than I expected...
BorrajaX
A: 

You don't have to mess with definition of child class (Class2). Just attach another property to its instance after creating the instance.

def Class1:
    def __init__(self):
        self.title = "randomTitle"
        self.anotherField = float()
        self.class2Field = Class2()
        self.class2Field.parent = self  # <- This is where you attach the pointer to parent.
        # . . . #

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

    def mess_with_parent(self):
        '''Example of access to instance-level property "parent"'''
        if hasattr(self, 'parent') and self.parent != None: 
            # instead of "!= None" insert your way of checking that .parent is right type
            self.parent.anotherField = 5.0
        # . . . #

a = Class1()
<instance of class1> = a.class2Field.parent
b = a.class2Field
<instance of class1> = b.parent
ddotsenko
If you set `z = Class2()`, then you'll get an error when you execute `z.mess_with_parent()` because the attribute `parent` doesn't exist.
eksortso
You are correct. (Spent a bit too much time time in JavaScript land, where that would actually work.)instead of if self.parent != None:there should be something like: if 'parent' in dir(self): if self.parent != None: # (or put your extra verification logic here):or:
ddotsenko