A class question
emorfo at gmail.com
emorfo at gmail.com
Mon Oct 29 10:04:39 EDT 2007
On Oct 29, 12:46 pm, "Martin Marcher" <mar... at marcher.name> wrote:
> 2007/10/29, Hrvoje Niksic <hnik... at xemacs.org>:
>
> > Sbe unpx inyhr, urer vf n cbffvoyr vzcyrzragngvba:
> > ...
>
> was that on purpose?
>
> martin
>
> --http://noneisyours.marcher.namehttp://feeds.feedburner.com/NoneIsYours
for humans:
For hack value, here is a possible implementation:
import inspect
def _find(frame, obj):
for name, value in frame.f_locals.iteritems():
if value is obj:
return name
for name, value in frame.f_globals.iteritems():
if value is obj:
return name
raise KeyError("Object not found in frame globals or locals")
class X:
def debug(self):
print ("Caller stores me in %s (among other possible places)"
% _find(inspect.currentframe(1), self))
>>> xyzzy = X()
>>> xyzzy.debug()
Caller stores me in xyzzy (among other possible places)
>>> a = b = X()
>>> a.debug()
Caller stores me in a (among other possible places)
>>> b.debug()
Caller stores me in a (among other possible places)
>>> X().debug()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in debug
File "<stdin>", line 8, in _find
KeyError: 'Object not found in frame globals or locals'
More information about the Python-list
mailing list