What does the following code output? from collections import namedtuple Point = namedtuple(‘Point’, [‘x’, ‘y’]) p = Point(3, 4) print(p.x, p[1], len(p))
Python DeveloperMedium
Python Developer — Medium
What does the following code output? from collections import namedtuple Point = namedtuple(‘Point’, [‘x’, ‘y’]) p = Point(3, 4) print(p.x, p[1], len(p))
Explanation
namedtuple creates an immutable tuple subclass with named fields; p.x accesses the first field (3), p[1] uses index access for the second field (4), and len(p) returns 2 as it has two elements.