Home >Backend Development >Python Tutorial >Python data structure: an underrated Namedtuple (2)

Python data structure: an underrated Namedtuple (2)

coldplay.xixi
coldplay.xixiforward
2020-10-20 17:10:303445browse

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

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!

Statement:
This article is reproduced at:juejin.im. If there is any infringement, please contact admin@php.cn delete

Related articles

See more