Python data structure: an underrated Namedtuple (2)
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.

VS Code is the full name Visual Studio Code, which is a free and open source cross-platform code editor and development environment developed by Microsoft. It supports a wide range of programming languages and provides syntax highlighting, code automatic completion, code snippets and smart prompts to improve development efficiency. Through a rich extension ecosystem, users can add extensions to specific needs and languages, such as debuggers, code formatting tools, and Git integrations. VS Code also includes an intuitive debugger that helps quickly find and resolve bugs in your code.

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.

VS Code not only can run Python, but also provides powerful functions, including: automatically identifying Python files after installing Python extensions, providing functions such as code completion, syntax highlighting, and debugging. Relying on the installed Python environment, extensions act as bridge connection editing and Python environment. The debugging functions include setting breakpoints, step-by-step debugging, viewing variable values, and improving debugging efficiency. The integrated terminal supports running complex commands such as unit testing and package management. Supports extended configuration and enhances features such as code formatting, analysis and version control.

Yes, VS Code can run Python code. To run Python efficiently in VS Code, complete the following steps: Install the Python interpreter and configure environment variables. Install the Python extension in VS Code. Run Python code in VS Code's terminal via the command line. Use VS Code's debugging capabilities and code formatting to improve development efficiency. Adopt good programming habits and use performance analysis tools to optimize code performance.
