Can a function access its own name?

Fredrik Lundh fredrik at pythonware.com
Sun Nov 20 10:03:18 CET 2005


bobueland at yahoo.com wrote:

> Look at the code below:
>
>     # mystringfunctions.py
>
>     def cap(s):
>         print s
>         print "the name of this function is " + "???"
>
>     cap ("hello")
>
>
> Running the code above gives the following output
>
>     >>>
>     hello
>     the name of this function is ???
>     >>>
>
> I would like the output to be
>
>     >>>
>     hello
>     the name of this function is cap
>     >>>
>
> Is that possible ?

    def cap(s):
        print s
        print "the name of this function is " + "cap"

yes, I'm serious.

if your question had been "can a function figure out the name of the
function that called it", the answer would have been different.

</F>






More information about the Python-list mailing list