Get reference to "owner"

Gerhard Häring gh_pythonlist at gmx.de
Tue Mar 5 02:42:52 EST 2002


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