Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Partially Emulating collections.namedtuple

If you want to learn more about collections.namedtuple, then check out Write Pythonic and Clean Code With namedtuple.

00:00 Partially Emulating collections.namedtuple. As a final example of how to take advantage of .__new__() in your code, you can push your Python skills and write a factory function that partially emulates collections.namedtuple.

00:16 The namedtuple() function allows you to create subclasses of tuple with the additional feature of having named fields for accessing the items in the tuple.

00:27 Next, you’ll see code that implements a named_tuple_factory() function that partially emulates this functionality by overriding the .__new__() method of a nested class called NamedTuple.

00:42 First, you import itemgetter from the operators module. This function allows you to retrieve items using their index in the containing sequence.

00:51 Next, named_tuple_factory() is defined. This function takes a first argument called type_name, which will hold the name of the tuple subclass that you want to create.

01:02 The *fields argument allows you to pass an undefined number of field names as strings. Here, you define a local variable to hold the number of named fields provided by the user. Here, you define a nested class called NamedTuple, which inherits from the built-in tuple class.

01:20 This provides a .__slots__ class attribute. This attribute defines a tuple for holding instance attributes. This tuple saves memory by acting as a substitute for the instance’s dictionary, which would otherwise play a similar role.

01:34 Next you implement .__new__() with cls as its first argument. This implementation also takes the *args argument to accept an undefined number of field values.

01:45 Then you define a conditional statement that checks if the number of items to store in the final tuple differs from the number of named fields. If that’s the case, then the conditional raises a TypeError with an error message.

02:02 This sets the .__name__ attribute the current class to the value provided by type_name. These lines define a for loop that turns every name field into a property that uses itemgetter() to return the item at the target index.

02:16 The loop uses the built-in setattr() function to perform this action. Note that the built-in enumerate() function provides the appropriate index value.

02:27 This line returns a new instance of the current class by calling super().__new__() as usual. These lines define a .__repr__() special method for the tuple subclass.

02:49 Finally, this line returns the newly created NamedTuple class,

02:57 To try the named_tuple_factory() out, start an interactive session in the directory containing the named_tuple.py file and run the following code.

03:11 First, create a new Point class by calling named_tuple_factory(). The first argument in this call represents the name that the resulting class object will use.

03:21 The second and third arguments are the named fields available in the resulting class. Then you create a Point object by calling the class constructor with the appropriate values for the x and y fields.

03:35 To access the value of each named field, you can use dot notation. You can also use indices to retrieve the values because your class is a tuple subclass.

03:50 Because tuples are an immutable data type in Python, you can’t assign new values to the point’s coordinates in place. If you try to do that, you get an AttributeError.

04:02 Finally, calling dir() with your point instance as an argument reveals that your object inherits all of the attributes and methods that regular tuples have in Python.

04:13 Now that you’ve covered all the content in this course, in the next section, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.