Why do only callable objects get a __name__?

John Ladasky john_ladasky at sbcglobal.net
Mon Nov 18 16:26:00 EST 2013


On Monday, November 18, 2013 1:11:08 PM UTC-8, Terry Reedy wrote:
> On 11/18/2013 3:13 PM, John Ladasky wrote:
> 
> > Of course, I have used __name__ for years in the common expression "if __name__ == '__main__'") to determine whether a particular module is being run or merely imported.
> 
> This true statement invalidates your subject line ;-). All modules have 
> a __name__. 

Yes, I thought about this before I posted.  I figured that, if I investigated further I would discover that there was a __main__ function that was being called.

> > 1. WHY do only callable objects get a __name__?
> 
> Why do you think this? Is there a mistake in the doc?

Quote below from http://docs.python.org/3/reference/datamodel.html:

=======================================================================

Callable types
These are the types to which the function call operation (see section Calls) can be applied:

User-defined functions
A user-defined function object is created by a function definition (see section Function definitions). It should be called with an argument list containing the same number of items as the function’s formal parameter list.

Special attributes:

Attribute	Meaning
__name__	The function’s name	Writable

=======================================================================

Perhaps I'm inferring too much from slightly-vague documentation, and my recent experience with objects that cannot be called?  Or perhaps the information that I need to read is somewhere in the documentation other than where I have looked?

Still puzzling over this...  I can easily hack a solution as Terry suggested, but it's not elegant, and that kind of thing bugs me.



More information about the Python-list mailing list