In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The main content of this article is "introduction to the specific usage of singledispatch library in Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the "introduction to the specific usage of the singledispatch library in Python".
Singledispatch
Imagine that you have a shape library with Circle, Square, and so on.
The Circle class has radius, Square has edges, and Rectangle has height and width. Our library already exists and we don't want to change it.
However, we want to add an area calculation to the library. If we don't share the library with others, we just add the area method so that we can call shape.area () regardless of what shape it is.
Although you can enter the class and add a method, this is a bad idea: no one wants their class to be added with a new method, and the program will go wrong in a strange way.
Instead, the singledispatch function in functools can help us.
Singledispatchdef get_area (shape): raise NotImplementedError ("cannot calculate area for unknown shape", shape)
The "base class" implementation of the get_area function reports an error. This ensures that if we have a new shape, we will explicitly report an error rather than return 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 writes a new shape that matches our code, they can implement get_area on their own.
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 calls get_area directly.
Print (get_area (shape))
This means that we can modify a lot of if isintance () / elif isinstance () code in this way without modifying the interface. Next time you want to modify if isinstance, you try `singledispatch!
At this point, I believe you have a deeper understanding of the "introduction to the specific usage of the singledispatch library in Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.