Finding the name of a function while defining it

Chris Angelico rosuav at gmail.com
Thu Dec 27 02:48:19 EST 2012


On Thu, Dec 27, 2012 at 6:26 PM, Abhas Bhattacharya
<abhasbhattacharya2 at gmail.com> wrote:
> During run-time, I can always use: function_name.__name__ (although that's kind of lame because it returns "function_name"). But if the function itself contains print(__name__) and I call the function, it returns __main__ (yes, __main__ itself, not the string "__main__") (which is the calling function).

That's because __name__ looks for that attribute on the module (aka
"global variable"), not the function. When you run your Python script
as an application, the module is called __main__.

ChrisA



More information about the Python-list mailing list