getting the name of a generator function

Duncan Booth duncan at NOSPAMrcp.co.uk
Wed Jun 11 14:47:20 CEST 2003


Oscar Rambla <tecnic at codidoc.com> wrote in
news:mailman.1055332280.26057.python-list at python.org: 

> 1)Is there a direct way to know the name of the function generator
> from the generator instance?
> 
> Ex:
> def f1():
>   yield 1
> 
> def f2():
>    yield f1()
> 
> g1= f2()
> g2 = g1.next()
> 
> "g2 is " g2?

Did you mean how do you get "f1" from g2? If so, try:

>>> inspect.getframeinfo(g2.gi_frame)[2]
'f1'

Although it escapes me WHY you would want to do this.

> 
> 2)How to know the name of the module that contains a base class
> definition from a class/subclass instance?

Using the __module__ attribute of the base class.


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list