object oriented programming question

Daniel Nogradi nogradi at gmail.com
Sun Dec 18 08:46:02 EST 2005


>
> Hello Daniel
>
You've certainly got a lot going on here.
>
> The heart of your question seems to be how a nested (inner) class _a can
> access
> its parent, x.  The short answer is that, in Python, it can't without some
> help.
>   _a and its instances are unaware of the context in which they are
> defined, so
> they hold no references to x or instance of x.  (Technically this is
> because the
> class is not created until after the statements in the class suite are
> executed)
>
> Before we get to that, though, note that you've made content an attribute
> of x's
> instances, not the x class.  So there is no x.content, only inst.content
> .  I'll
> assume this was a typo, and you intended inst.content.  If this
> distinction is
> mysterious you may want to check: http://docs.python.org/tut/node11.html
>
> Now, back to the question of how instance a could get a reference to
> inst.  The
> simplest way to program this is to give it the reference explicitly:
>
>
> class _a:
>
>      def func(self):
>          """I would like to do something
>          with ´content´ here.
>          """
>          print self.__parent__.content
>
>
> class x:
>      def __init__(self):
>          self.content = ["I'm", "self.", "content"]
>
>      a = _a()
>
>   >>> inst = x()
>   >>> inst.a.__parent__ = inst # give inst.a a reference to its parent
>   >>> inst.a.func()
>   ["I'm", 'self.', 'content']
>   >>>
>
>
>
> There is a way to automate "inst.a.__parent__ = inst", although it's not
> ideal
> "getting starting with Python objects" material.  The solution is to have
> _a
> implement the descriptor protocol (see:
> http://users.rcn.com/python/download/Descriptor.htm), and make the classes
> "new-style", i.e., derived from 'object':
>
> class _a(object):
>
> ##    def __call__(self, v):  Method is irrelevant to this discussion
> ##        print v
>
>      def func(self):
>          """I would like to do something
>          with ´content´ here.
>          """
>          print self.__parent__.content
>
>
>      def __get__(self, obj, cls):
>          """Store a reference to the referring obj"""
>          if isinstance(obj, cls):
>              self.__parent__ = obj
>          return self
>
> class x(object):
>      def __init__(self):
>          self.content = ["I'm", "self.", "content"]
>
>      a = _a()
>
>
>   >>> inst = x()
>   >>> inst.a.func() #no need to set inst.a.__parent__ = inst manually now
>   ["I'm", 'self.', 'content']
>   >>>
>
> HTH
>
> Michael




Hi Michael,

Thanks for the detailed answer, and yes, it was a typo, I meant

inst = x()
inst.a(5)
inst.a.func()

Following your example I could put together exactly what I wanted, thank you
very much.

Daniel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20051218/89464942/attachment.html>


More information about the Python-list mailing list