On Thu, Oct 26, 2017 at 12:25 PM, qrious <mit...@juno.com> wrote: > > Class1 is instantiated in Class2 as follows. Class2 also contains another > variable, say: > > class Class2: > class1 = Class1() > a = 0 > > I want to create a method myDef() in Class1 that can read or write to a. How > do I access a from within myDef() to access a? > > Calling Class2.a is not an option as Class1 does not have any knowledge about > its container class a priori. Also, this will hardcode the path. I want to > know of a built-in method (kind of equivalent of super() for inheritance) > that will give me the container class reference, Class2 in this case. > > Thanks.
There isn't any. Relationships like this are one-way; you can go from Class2 to its attribute named "class1", which is an instance of Class1, but you can't go back the other way. There could be multiple ways to get to that same object, so there's no way for it to know which one you want. Now, there MAY be some strange and arcane magic that you can do as you construct that object to figure out what class it's in. But generally, you should avoid that kind of thing. There is (almost certainly) a better way to accomplish whatever it is you're aiming for. ChrisA -- https://mail.python.org/mailman/listinfo/python-list