Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

In the python documentation, an instance of a named_tuple is created by using the below code:

Point = named_tuple('Point', ['x', 'y'])

p = Point(11, y=22)

Is it necessary to use the same variable name as the type name of named_tuple? What is the benefit of doing it?

1 Answer

0 votes
by (36.8k points)

If you use different names it will lead to unnecessary confusion.

> from collections import namedtuple

> Foo = namedtuple('Bar', ['x', 'y'])

> a = Foo(1, 2)

> print(a)

Bar(x=1, y=2)

 Do check out Data Science with Python course which helps you understand from scratch 

Browse Categories

...