I see that the Python syntax for a namedtuple is:
Point = namedtuple('Point', ['x', 'y'])
Why isn't it simpler like so:
Point = namedtuple(['x','y'])
Its less verbose,
I see that the Python syntax for a namedtuple is:
Point = namedtuple('Point', ['x', 'y'])
Why isn't it simpler like so:
Point = namedtuple(['x','y'])
Its less verbose,
Because
namedtuple
is a function that returns a class. To do that, it is actually rendering a string template and callingeval
. To build the string, it needs all the arguments beforehand.You need to include the relevant context as arguments to
namedtuple
for that to happen. If you don't provide the class name argument, it would need to guess. Programming languages don't like to guess.With the rules of the Python language, the
namedtuple
function within this expression....doesn't have access to variable name (
Point
) that the result is stored in once the expression has been executed. It only has access to the elements of the list provided as its argument (and variables that have been defined earlier).namedtuple is a factory, returning a class. Consider only expression:
What would be the name of class returned by this expression?
In general, objects don't know what variables they are assigned to:
That's a problem for named tuples. We have to pass in the class name to the namedtuple() factory function so that the class can be given a useful name, docstring, and __repr__ all of which have the class name inside it.
These reason it seems strange to you is that normal function and class definitions are handled differently. Python has special syntax for
def
andclass
that not only creates functions and classes, but it assigns their metadata (name and docstring) and assigns the result to a variable.Consider what
def
does:The keyword
def
takes care of several things for you (notice that the word "square" will be used twice):The same is also true for classes. The
class
keyword takes care of multiple actions that would otherwise repeat the class name:The underlying steps repeat the class name (notice that the word "Dog" is used twice):
Named tuples don't have the advantage of a special keyword like
def
orclass
so it has to do the first to steps itself. The final step of assigning to a variable belongs to you. If you think about it, the named tuple way is the norm in Python whiledef
andclass
are the exception:You are not the first to notice this. PEP 359 that suggested we add a new keyword,
make
, that could allow any callable to gain the auto-assignment capabilities ofdef
,class
, andimport
.would be translated into the assignment:
In the end, Guido didn't like the "make" proposal because it caused more problems than it solved (after all, it only saves you from making a single variable assignment).
Hope that helps you see why the class name is written twice. It isn't really duplication. The string form of the class name is used to assign metadata when the object is created, and the separate variable assignment just gives you a way to refer to that object. While they are usually the same name, they don't have to be :-)
The class should have a name and know it. And it doesn't see the variable you assign it to, so it can't use that. Plus you could call it something else or even nothing at all:
Speaking of simpler syntax, you can also write it like this: