python video tutorialThe column continues to take you to understand the Namedtuple of Python data structure.
Part 1Python data structure: an underestimated Namedtuple (1) After talking about some basic usage of namedtuple, this article continues.
What is the difference between namedtuples and data classes?
Function
Prior to Python 3.7, you could create a simple data container using any of the following methods:
- namedtuple
- General Class
- Third party libraries,
attrs
If you want to use regular classes, that means you will have to implement a few methods. For example, a regular class would require an __init__
method to set properties during class instantiation. If you want the class to be hashable, that means implementing a __hash__
method yourself. In order to compare different objects, you also need to __eq__
implement a method. Finally, to simplify debugging, you need a __repr__
method.
Let's use regular classes to implement our color use case.
class Color: """A regular class that represents a color.""" def __init__(self, r, g, b, alpha=0.0): self.r = r self.g = g self.b = b self.alpha = alpha def __hash__(self): return hash((self.r, self.g, self.b, self.alpha)) def __repr__(self): return "{0}({1}, {2}, {3}, {4})".format( self.__class__.__name__, self.r, self.g, self.b, self.alpha ) def __eq__(self, other): if not isinstance(other, Color): return False return ( self.r == other.r and self.g == other.g and self.b == other.b and self.alpha == other.alpha )复制代码
As above, you need to implement many methods. You just need a container to hold your data for you without worrying about distracting details. Again, a key difference in why people prefer implementation classes is that regular classes are mutable.
In fact, the PEP that introduces Data Classes
refers to them as "mutable namedtuples with default values" (Translator's Note: Data Class introduced in python 3.7, reference: docs.python.org/zh-cn/3/lib…
Now, let’s see how to do it using data classes
.
from dataclasses import dataclass ...@dataclassclass Color: """A regular class that represents a color.""" r: float g: float b: float alpha: float复制代码
Wow! That’s it Simple. Since there is no __init__
, you just define the properties after the docstring. Additionally, they must be annotated with type hints.
In addition to being mutable, data classes can Optional fields are provided out of the box. Suppose our Color class does not require an alpha field. Then we can make it optional.
from dataclasses import dataclassfrom typing import Optional ...@dataclassclass Color: """A regular class that represents a color.""" r: float g: float b: float alpha: Optional[float]复制代码
We can instantiate it like this:
>>> blue = Color(r=0, g=0, b=255)复制代码
Since They are mutable, so we can change any field we want. We can instantiate it like this:
>>> blue = Color(r=0, g=0, b=255) >>> blue.r = 1 >>> # 可以设置更多的属性字段 >>> blue.e = 10复制代码
In comparison, namedtuple
has no optional fields by default .To add them we need a little trickery and some metaprogramming.
Tip: To add the __hash__
method you need to make it immutable by setting unsafe_hash
to True
:
@dataclass(unsafe_hash=True)class Color: ...复制代码
Another difference is that unpacking is a built-in function of namedtuples (first-class citizen). If you want the data class
to have If you have the same behavior, you must implement yourself.
from dataclasses import dataclass, astuple ...@dataclassclass Color: """A regular class that represents a color.""" r: float g: float b: float alpha: float def __iter__(self): yield from dataclasses.astuple(self)复制代码
Performance comparison
Only comparing functions is not enough, namedtuple and data classes are also different in performance. The data class implements dict based on pure Python. This makes them faster when accessing fields. Namedtuples, on the other hand, are just regular extended tuples. This means that their implementation is based on faster C code and has a smaller memory footprint.
To prove At this point, please consider running this experiment on Python 3.8.5.
In [6]: import sys In [7]: ColorTuple = namedtuple("Color", "r g b alpha") In [8]: @dataclass ...: class ColorClass: ...: """A regular class that represents a color.""" ...: r: float ...: g: float ...: b: float ...: alpha: float ...: In [9]: color_tup = ColorTuple(r=50, g=205, b=50, alpha=1.0) In [10]: color_cls = ColorClass(r=50, g=205, b=50, alpha=1.0) In [11]: %timeit color_tup.r36.8 ns ± 0.109 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [12]: %timeit color_cls.r38.4 ns ± 0.112 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each) In [15]: sys.getsizeof(color_tup) Out[15]: 72In [16]: sys.getsizeof(color_cls) + sys.getsizeof(vars(color_cls)) Out[16]: 152复制代码
As above, data classes are slightly faster to access fields in, but they take up more memory space than nametuples.
How to add type hints to namedtuple
Data classes use type hints by default. We can also put them on namedtuples. We can annotate Color tuples by importing the Namedtuple annotation type and inheriting from it .
from typing import NamedTuple ...class Color(NamedTuple): """A namedtuple that represents a color.""" r: float g: float b: float alpha: float复制代码
Another detail that may not have been noticed is that this approach also allows us to use docstrings. If we enter help(Color) we will be able to see them.
Help on class Color in module __main__:class Color(builtins.tuple) | Color(r: float, g: float, b: float, alpha: Union[float, NoneType]) | | A namedtuple that represents a color. | | Method resolution order: | Color | builtins.tuple | builtins.object | | Methods defined here: | | __getnewargs__(self) | Return self as a plain tuple. Used by copy and pickle. | | __repr__(self) | Return a nicely formatted representation string | | _asdict(self) | Return a new dict which maps field names to their values.复制代码
How to add optional default values to namedtuple
In the previous section, we learned that data classes can have optional values. In addition, I mentioned that to imitate the same behavior, namedtuple
requires some technical modifications. It turns out that we can use inheritance as shown in the following example.
from collections import namedtupleclass Color(namedtuple("Color", "r g b alpha")): __slots__ = () def __new__(cls, r, g, b, alpha=None): return super().__new__(cls, r, g, b, alpha)>>> c = Color(r=0, g=0, b=0)>>> c Color(r=0, g=0, b=0, alpha=None)复制代码
Conclusion
Tuple is a very powerful data structure. They make our code cleaner and more reliable. Despite the fierce competition with the new data classes
, they still have a large number of scenarios available. In this tutorial, we learned several ways to use namedtuples
and we hope you can use them.
Related free learning recommendations: python video tutorial
The above is the detailed content of Python data structure: an underrated Namedtuple (2). For more information, please follow other related articles on the PHP Chinese website!

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.