Obtaining "the" name of a function/method

Cameron Simpson cs at zip.com.au
Sun Nov 17 19:19:35 EST 2013


On 17Nov2013 13:47, Tim Chase <python.list at tim.thechases.com> wrote:
> On 2013-11-17 11:34, Ned Batchelder wrote:
> > Functions have a __name__ attribute, which is the name they were
> > defined as:
> > 
> >     >>> def foo(): pass  
> >     ...
> >     >>> foo.__name__  
> >     'foo'
> >     >>> bar = foo
> >     >>> bar.__name__  
> >     'foo'
> 
> which they have even in less-than-useful situations:
> 
>   (lambda s: s.lower()).__name__
> 
> accurately returns that its name is "<lambda>".  So you get what you
> pay for ;-)

Also, it is documented that you may assign to __name__.

I've started doing that in a few cases where I have a table of lambdas mapped
by operation names; I've started labelling the lambdas to get better debugging.
-- 
Cameron Simpson <cs at zip.com.au>

Ride with a llama and you never ride alone.
        - Jeff Earls, DoD #0530, <jearls at tekig6.pen.tek.com>



More information about the Python-list mailing list