'parent object'?

sebastien s.keim at laposte.net
Wed Jun 26 06:32:01 EDT 2002


> Is there, like 'self, also a 'parent' object of some sort? 
> The problem i'm having is that i have a class, which contains a list of
> instances of another class. How can i let this 'childclass' call a function
> which is on the 'parent class'?

As have said other posters, you should probably avoid this kind of design.
But if you absolutely need it, here is a short hack for Python 2.2 that 
use attribute descriptors:

class Parent(object):
    class Child(object):
        def __get__(self, obj, cls):
            self.__parent = obj
            return self
        def hello(self):
            print "Hello, I'm",self,"and my father is",self.__parent
    child = Child()

x = Parent()
x.child.hello()



More information about the Python-list mailing list