Singledispatch used addition method in Python retroactively

For more information on Python to solve the problem in our library covering seven PyPI series of articles.

Python is today the most used popular programming language one, because: it is open source, it has a wide range of applications (such as Web programming, business applications, games, scientific programming, etc.), it has a vibrant and dedicated community support it. This community is our Python Package Index provided in such a large (PyPI), the cause of a variety of software packages for the expansion and improvement of Python. And solve the inevitable problems.

In this series, we will introduce seven can help you solve common problems PyPI Python library. Today we will study singledispatch , this is a place where you retroactively added to the Python library library methods.

singledispatch

Imagine you have a Circle, Square and other types of "shape" of the library.

Circle class having a radius, Square edged, Rectangle high and wide. Our library already exists, we do not want to change it.

However, we would like to add a library area calculation. If we do not share them with others this library, we simply add areamethod, so we can call shape.area()without caring about what shape.

Although you can enter the class and add a method, but it is a bad idea: Nobody wants their class will be to add new methods, the program will go wrong because of a strange way.

Instead, functools the singledispatchfunction can help us.

@singledispatch
def get_area(shape):
    raise NotImplementedError("cannot calculate area for unknown shape",
                              shape)
复制代码

get_area"Base class" Functions of the error. This ensures that if we see a new shape, we will definitely return error rather than a meaningless result.

@get_area.register(Square)
def _get_area_square(shape):
    return shape.side ** 2
@get_area.register(Circle)
def _get_area_circle(shape):
    return math.pi * (shape.radius ** 2)
复制代码

The advantage of this approach is that if someone wrote a code to match our new shape, they can achieve their own get_area.

from area_calculator import get_area

@attr.s(auto_attribs=True, frozen=True)
class Ellipse:
    horizontal_axis: float
    vertical_axis: float

@get_area.register(Ellipse)
def _get_area_ellipse(shape):
    return math.pi * shape.horizontal_axis * shape.vertical_axis
复制代码

Call get_area it directly.

print(get_area(shape))
复制代码

This means we can be a large number if isintance()/ elif isinstance()code changes in this manner, without modifying the interface. The next time you want to change if isinstance, you try `singledispatch!

In the next article in this series, we will introduce tox, an automated testing tool for Python code.

Recalling the previous articles in this series:


via: opensource.com/article/19/…

Author: Moshe Zadka topics: lujun9972 Translator: geekpi proofread: wxy

This article from the LCTT original compiler, Linux China is proud

Guess you like

Origin blog.csdn.net/weixin_34220623/article/details/91405933
Recommended