Accessing func_name from inside a function

Ron Adam rrr at ronadam.com
Sun Mar 26 14:07:25 EST 2006


Alex Martelli wrote:

> Personally, I'd rather have a 3.0 keyword referring to "the current
> object" (module for module toplevel code, class for classbody toplevel
> code, function for code within a function) -- say for the sake of
> argument the keyword is 'current'; this would allow current.__name__ to
> have the obvious meaning, and would also allow a few more neat things
> (such as natural access to current.__doc__).

That was my thought too, that what is really desired is a *obvious* way 
to get a dependable reference to the current function as you describe here.

A quick experiment...  with an obvious result:

>>>> def foo(): print foo.__name__
> ...
>>>> foo()
> foo
>>>> boo = foo
>>>> boo()
> foo
>>>> boo.__name__
> 'foo'
>>>> foo = None
>>>> boo()
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
>   File "<stdin>", line 1, in foo
> AttributeError: 'NoneType' object has no attribute '__name__'

A "Current" key word would fix this.  Or possibly "This" which would be 
short for "This object".

This may also relate to suggestions to reduce the need for having self 
in the argument list of methods.  So if a keyword "This" could mean this 
method or function, then "Self" could mean this class.  Hmmm, methods 
that use "Self" in this way might run into problems, but I havn't had 
enough coffee to think it though. ;-)

Cheers,
    Ron




More information about the Python-list mailing list