In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about what is the most unpopular module of Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Starting today, open a series of updates for unpopular modules.
The collections module is one of the most unpopular modules, with few people, but it is very powerful.
Collections provides four important data types that will be more or less exposed in actual development, and by using these data types, you can make your code more elegant and authentic.
Today, let's introduce the type of namedtuple.
Namedtuple
Namedtuple is a subclass of the tuple type, so in essence it is a tuple type, inheriting all the features of the tuple. What makes namedtuple special is that you can access the elements in the tuple by name, like a dictionary, and access value through key.
Elements in a previously accessed tuple must be accessed through an index
> x, y = 1pm 2
> point = (x, y)
> point [0]
one
> point [1]
two
Now that you can access it through the property name, let's see how to define a namedtuple
Define from collections import namedtuple
# first define a namedtuple class
The name of # class is "Point"
# it has two attributes x and y
> Point = namedtuple ('Point', [' x','y'])
# namedtuple initialization
> p = Point (Xero11, YB22)
# like tuple type, accessed through subscript index, which is equivalent to p = (11,22)
> p [0] + p [1]
thirty-three
# access through field properties, which is unique to namedtuple
> > p.x + p.y
thirty-three
It seems that it is troublesome to use namedtuple. Unlike tuple, where a tuple object is defined directly with p = (11,22), what scenario will namedtuple be used in?
The answer is when using tuple is not readable, but you don't want to use class to customize classes.
For example, there is such a set of data
Bob = ('Bob', 30,' male')
In fact, you don't know what the three elements mean, maybe you can guess, but only by guess. how can it be more readable? In fact, we can customize a class to abstract this set of data.
Class Person:
Def _ _ init__ (name, age, gender):
Self.name = name
Self.age = age
Self.gender = gender
Bob = Person ('Bob', 30,' male')
Through the Person class, you can see at a glance that Bob corresponds to the gender field corresponding to name,30 and age,male.
But in doing so, although it is a little more readable, the code is more cumbersome, and more importantly, the cost of creating such an object is much higher than that of pure tuples.
Namedtuple can just solve this problem, it not only inherits the good performance of tuple, but also has the characteristics of readability.
From collections import namedtuple
Person = namedtuple ("Person", "name age gender")
Bob = Person (name='Bob', age=30, gender='male')
> bob [0]
'Bob'
> bob.name
'Bob'
> bob.age
thirty
> bob [1]
thirty
Have you noticed that namedtuple can be thought of as a simple custom class that can specify properties, but not methods like classes defined by class. Therefore, when considering that if you define a class, there is no need to define methods in the class, you can actually use namedtuple instead.
That's what namedtuple is for. Do you understand?
After reading the above, do you have any further understanding of what is the most unpopular module of Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.