On Apr 6, 9:53 am, Reckoner <recko...@gmail.com> wrote: > hi, > > I have the following problem: I have two objects, say, A and B, which > are both legitimate stand-alone objects with lives of their own. > > A contains B as a property, so I often do > > A.B.foo() > > the problem is that some functions inside of B actually need A > (remember I said they were both standalone objects), so I have to > often do: > > A.B.foo_func(A) > > Which is kind of awkward. > > Is there some way that B.foo_func() could somehow know that it was > called as a property of A in this way? > > Note that I'm looking for the calling object and NOT the calling > function. > > Thanks in advance.
Hi Reckoner, I believe this does what you want. It's an advanced technique and not available in all OO languages. class ClsA( object ): def __init__( self ): self.inst= None def __get__( self, instance, owner ): self.inst= instance return self def submethA( self, arg ): print( 'submethA %r, instance %r'% ( arg, self.inst ) ) class ClsB( object ): A= ClsA( ) def methA( self, arg ): print( 'methA %r'% arg ) b= ClsB( ) b.methA( 'this' ) b.A.submethA( 'that' ) #Output: ''' methA 'this' submethA 'that', instance <__main__.ClsB object...> ''' There's a small discussion in another today's thread, 'group several methods under an attribute'. -- http://mail.python.org/mailman/listinfo/python-list