Why do only callable objects get a __name__?
John Ladasky
john_ladasky at sbcglobal.net
Mon Nov 18 16:02:26 EST 2013
On Monday, November 18, 2013 12:43:28 PM UTC-8, Ian wrote:
> Classes and functions are frequently kept in module namespaces, where
> they are known by a specific name. The intent is that the __name__
> attribute should match that name by which it is commonly referred.
>
>
>
> Specific instances are not typically widely referred to by set names
>
> in this way. They are more commonly stored in variables that are used
>
> to hold a wide variety of objects.
>
>
>
> In the namedtuple example that you give, it seems that you would want
> the names of all instances of the ANamedTuple class to be the same
> "ANamedTuple", and I really don't see what the purpose of giving them
> all the same name would be.
I am implementing a state machine. The outputs of the various states in the machine have variable contents. I started by making dictionaries for each state output, but I soon tired of the bracket-and-quote-mark syntax for
referring to the contents of these state output dictionaries. That's why I am switching to named tuples. It doesn't affect the __name__ issue, since dictionaries also cannot be called.
I want to capture the names of the executed states in a record of the state machine's history. This information is already encoded in the namedtuple's type.
> > 2. If I created a superclass of namedtuple which exposed type(namedtuple).__name__ in the namespace of the namedtuple itself, would I be doing anything harmful?
>
> Probably not.
I just thought I would ask.
> But why not just invent your own name attribute rather
> than shadow the one that Python designates for classes and functions?
I will never write to this attribute, only read it. And since the information I want is already there (albeit in a strange place), I am inclined to use it.
More information about the Python-list
mailing list