Get reference to "owner"

Robert Oschler Oschler at earthlink.net
Tue Mar 5 10:05:58 EST 2002


Gerhard,

Excellent!!!  Never expected to find a language that could do this and it
something I've wanted very badly for a long time.  Yes that's exactly what I
meant.  B knows about the A that owns it without having to artificially pass
a reference of A to B during construction.

Thanks again.


Gerhard Häring <gh_pythonlist at gmx.de> wrote in message
news:mailman.1015314249.8983.python-list at python.org...
> Le 05/03/02 à 08:26, Gerhard Häring écrivit:
> > Le 05/03/02 à 01:41, Robert Oschler écrivit:
> > > I've never seen this feature in any programming language I've worked
in but
> > > it can't hurt to ask.  Can an aggregated class object get a reference
to the
> > > class object that aggregates it?  So if ClassAggregator contained an
> > > instance of ClassAggregatee is there a Python syntax/construct that a
> > > function in ClassAggregatee could use to get a reference to the
instance of
> > > ClassAggregator that contained it?
> >
> > [unfinished code]
>
> If I understood you correctly, this might do the trick:
>
> import sys
>
> class A:
>     def __init__(self):
>         self.b = B()
>
> class B:
>     def __init__(self):
>         print sys._getframe(1).f_locals['self']
>
> a = A()
>
> It even works with Jython, so it's reasonably portable.
>
> Gerhard
> --
> This sig powered by Python!
> Außentemperatur in München: 1.8 °C      Wind: 1.6 m/s
>






More information about the Python-list mailing list