Why do only callable objects get a __name__?

John Ladasky john_ladasky at sbcglobal.net
Tue Nov 19 01:36:34 EST 2013


Thanks for your replies, Steven.  Between this post and your other post, you wrote a lot.

On Monday, November 18, 2013 3:21:15 PM UTC-8, Steven D'Aprano wrote (and I quote, edited, and sometimes out of order):

> So if you have any 
> thought that "the name of an object" should be the name of the variable, 
> scrub that from your head, it will never fly.

I certainly don't, as you would see from the title of my thread from two days ago, which lead to this thread: "Obtaining 'the' name of a function/method".

https://groups.google.com/forum/#!topic/comp.lang.python/bHvcuXgvdfA

It took me a few months to understand the Pythonic concept of binding names to anonymous objects when I first started with Python... but that was quite a few years ago.


> For the 
> rest of this post, any time I talk about a name, I always mean the name 
> an object knows itself by, and never the variable name it is bound to (if 
> there is such a variable name).

[snip]

> For functions and classes, such names are especially useful, for 
> debugging and error messages:

If you read my response to your other post, you will see that debugging is definitely one of the reasons I'm undertaking this approach.  But even after the debugging is complete, I will still find it helpful for logging and monitoring purposes.

> > from collections import namedtuple
> > MyNamedTupleClass = namedtuple("ANamedTuple", ("foo", "bar"))
> 
> Here you define a class, called "ANamedTuple". Unfortunately, it doesn't 
> use the standard class syntax, a minor limitation and annoyance of 
> namedtuples, and so you're forced to give the class name "ANamedTuple" 
> explicitly as an argument to the function call. But the important thing 
> here is that it is a class.

OK, that helps.  I just had a look at the namedtuple source code.  Part of my conceptual problem stems from the fact that namedtuple() is what I think people call a "class factory" function, rather than a proper class constructor.  I'll read through this until I understand it.




More information about the Python-list mailing list