introspection inquiry

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Feb 20 13:44:03 EST 2005


Robin Becker wrote:
> import inspect
> class A:
>   _class_name=inspect.currentframe().f_code.co_name
>   def __init__(self,text,_defining_class_name=_class_name):
>     print 'text=',text,'_defining_class_name=',_defining_class_name
> 
> class B(A):
>   pass
> b=B('aaa')

That won't work, if you, say, wanted to print out the name of the
class the constructor was defined in for a whole chain of
constructors. Which is about the only case I can think of where
this would be useful.

> could work as well, but if we only need the local name why not just 
> insert directly.

To be honest, that is what I have done every time I have needed
something like this.

I've only used metaclasses in production code once and I still debate
whether that case is a good idea or not.
-- 
Michael Hoffman



More information about the Python-list mailing list