I see that the Python syntax for a namedtuple is:
Point = namedtuple(\'Point\', [\'x\', \'y\'])
Why isn\'t it simpler like so:
Because namedtuple
is a function that returns a class. To do that, it is actually rendering a string template and calling eval
. 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..
>>> Point = namedtuple(['x','y'])
..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).