Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namedtuple Class Name

Tags:

python-3.x

Just wondering that when we create a namedtuple object, why we always make the new object name (on the left) as the same as the namedtuple object name (on the right)?

I have tried an experiment to make them different names as below and then add in fields data using different objects name as below:

Running Code Example

>>> latlong = namedtuple('good', 'lat long')
>>> latlong(1, 2)
good(lat=1, long=2)
>>> good(1,2)
latlong(lat=1, long=2)

So when I add fields using latlong, it returns the good(), when I use good, it return latlong. Why they return different name each time? shouldn't them always return the the original object name which is good?

Thank You!
Erik

like image 236
Erik.Z Avatar asked Jan 31 '26 14:01

Erik.Z


1 Answers

The name passed as first parameter is the one that gets stored inside the named tuple, as its __name__ attribute - and is used when you call repr on such a tuple. (You, or, in this case, the interactive interpreter - which makes a repr call to get you what you s printed).

The name you assign the namedtuple to - that is, the name on the left side of = is just any name you choose to use in your code - like you can name any other Python object.

You can even call a single namedtuple class a lot of names, by simply doing:

N = Name = NameAndAddress = namedtuple("NameAndAddress", "name address")

You can use the same object by using any of the assigned names. Or you can store a lot of namedtuples in a list, and give no explict name to any of them - just like any other object, what variable name holds a reference to an object makes no difference.

That is a bit unfortunate, because for better readability, we like to have the namedtuple "internal" name to be the same we use to reference it. Python, has no way of cleanly creating such an assignment - unless it is made inside a class body. In other words: there is no mechanism in the language that can make the expression on the right side of = to know which name it is being assigned to (on the left side of =) - so namedtuple signature requires you to explicitly type its name as a string. But despite that, there are no mechanisms or checks to verify if assigned and internal name are the same.

Just as a side comment: if you are assigning names inside a class body, there are mechanisms in Python that allow you to "tell" the assigned object its name (which will be a class attribute). Up to Python 3.5, you have to do that on the class's metaclass - or in a class decorator. In Python 3.6, the assigned object may be itself a descriptor, whose class implements a special method named __set_name__. If so, that method is called at class instantiation and passed the class object and the attribute name - the code in the method can then set the name in the instance.

like image 72
jsbueno Avatar answered Feb 02 '26 15:02

jsbueno



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!