Table of Contents
What is the difference between namedtuples and data classes?
Function
Performance comparison
How to add type hints to namedtuple
How to add optional default values ​​to namedtuple
Conclusion
Home Backend Development Python Tutorial Python data structure: an underrated Namedtuple (2)

Python data structure: an underrated Namedtuple (2)

Oct 20, 2020 pm 05:10 PM
python

python video tutorialThe column continues to take you to understand the Namedtuple of Python data structure.

Python data structure: an underrated Namedtuple (2)

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
        )复制代码
Copy after login

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复制代码
Copy after login

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]复制代码
Copy after login

We can instantiate it like this:

>>> blue = Color(r=0, g=0, b=255)复制代码
Copy after login

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复制代码
Copy after login

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:
    ...复制代码
Copy after login

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)复制代码
Copy after login

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复制代码
Copy after login

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复制代码
Copy after login

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.复制代码
Copy after login

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)复制代码
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Golang vs. Python: Performance and Scalability Golang vs. Python: Performance and Scalability Apr 19, 2025 am 12:18 AM

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run python with notepad How to run python with notepad Apr 16, 2025 pm 07:33 PM

Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".

See all articles