Home > Backend Development > Python Tutorial > Awesome! Recommend eight cool Python decorators!

Awesome! Recommend eight cool Python decorators!

WBOY
Release: 2023-04-19 11:49:03
forward
1473 people have browsed it

Awesome! Recommend eight cool Python decorators!

Preface

One of the great things about the Python programming language is that it packs all the features into a small package that are very useful.

Many features can completely change the functionality of Python code, making the language even more flexible. If used correctly, some of these features can effectively reduce the time required to write a program.

A good example of achieving these goals are Python’s decorators.

Decorators

Decorators are functions that can be used to change the behavior of a Python function object. They can be applied to classes and functions and can do a lot of really interesting things!

Decorators can be used to shorten your code, speed it up, and completely change the way your code behaves in Python.

Needless to say, this can certainly come in handy! Today I want to show off some decorators that I think are worth checking out.

There are a lot of decorators, but I've chosen a few that I think have the coolest functionality.

1. @lru_cache

The first decorator in this list comes from the functools module.

This module is included in the standard library and is very easy to use. It also contains cooler features than this decorator, but this decorator is definitely my favorite.

This decorator can be used to speed up successive runs of a function using caching. Of course, this should be used with some caveats about caching in mind, but in general use cases, most of the time this decorator is worth using.

It's great to be able to speed up your code with a simple decorator.

A good example of a function that could benefit from such a decorator is a recursive function, such as a function that computes factorials:

def factorial(n):
 return n * factorial(n-1) if n else 1
Copy after login

Recursion can be very difficult in terms of computation time , but adding this decorator helps significantly speed up successive runs of this function.

@lru_cache
def factorial(n):
 return n * factorial(n-1) if n else 1
Copy after login

Now whenever we run this function, the first few factorial calculations will be saved to the cache.

So the next time we call this function, we just need to calculate the factorial after the factorial we used before.

Of course, not all factorial calculations will be saved, but it's easy to see why this decorator is a good application to speed up some naturally slow code.

2. @jit

JIT is the abbreviation of Just In Time. Usually whenever we run some code in Python, the first thing that happens is compilation.

This compilation incurs some overhead because the type is allocated memory and stored as an unallocated but named alias. With just-in-time compilation, we compile only at execution time.

In many ways, we can think of this as something similar to parallel computing, where the Python interpreter handles two things at the same time to save some time.

The Numba JIT compiler is famous for bringing this concept to Python. Similar to @lru_cache, this decorator can be called very easily and immediately improve the performance of your code. The Numba package provides jit decorators that make it easier to run more intensive software without having to go into C.

The following case uses the @jit decorator to speed up Monte Carlo method calculations.

from numba import jit
import random
@jit(nopython=True)
def monte_carlo_pi(nsamples):
 acc = 0
 for i in range(nsamples):
 x = random.random()
 y = random.random()
 if (x ** 2 + y ** 2) < 1.0:
 acc += 1
 return 4.0 * acc / nsamples
Copy after login

3. @do_twice

do_twice The function of the decorator is similar to its name. This decorator can be used to run a function twice with one call. This certainly has some uses, I find it particularly useful for debugging.

It can be used to measure the performance of two different iterations. Taking Functools as an example, we can run a function twice to check for improvements. This function is provided by the decorator module in Python, which is located in the standard library.

from decorators import do_twice
@do_twice
def timerfunc():
%timeit factorial(15)
Copy after login

4. @count_calls

The count_calls decorator can be used to provide information about how many times a function is used in the software.

Like do_twice, this can certainly come in handy when debugging.

When added to a given function, we will receive an output telling us how many times the function has been run each time it is run. This decorator is also in the decorator module of the standard library.

from decorators import count_calls
@count_calls
def function_example():
print("Hello World!")
function_example()
function_example()
function_example()
Copy after login

5. @dataclass

To save time writing classes, one of the best decorators I have always used is the @dataclass decorator.

This decorator can be used to quickly write common standard methods in classes that will usually be found in the classes we write.

This decorator comes from the dataclass module. This module is also in the standard library, so no PIP is required to try this example!

from dataclasses import dataclass
@dataclass
class Food:
name: str
unit_price: float
stock: int = 0
 def stock_value(self) -> float:
 return(self.stock * self.unit_price)
Copy after login

This code will automatically create an initialization function init() with the positional parameters needed to populate the data in the class.

They will also be automatically provided to self, so there is no need to write a long function to put some data parameters into the class.

6. @singleton

In order to understand the purpose of the singleton decorator, we first need to understand what a singleton (singleton) is. In a sense, singletons are a version of global variable types.

这意味着类型被定义为只存在一次。尽管这些在 C++ 等语言中很常见,但在 Python 中却很少见到。使用单例,我们可以创建一个只使用一次的类并改变类,而不是通过初始化来构造新的类型。

通常,单例装饰器是由用户自己编写的,实际上并不是导入的。

这是因为单例仍然是对我们单例装饰器中提供的模板的引用。我们可以命名一个单例函数并编写一个包装器,以便在我们的类上使用这个装饰器:

def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
@singleton
class cls:
def func(self):
Copy after login

另一种方法是使用元类!

7. @use_unit

在科学计算中经常派上用场的一种装饰器是 @use_unit 装饰器。

此装饰器可用于更改返回结果的表示单位。这对于那些不想在数据中添加度量单位但仍希望人们知道这些单位是什么的人很有用。

这个装饰器也不是在任何模块中真正可用,但它是非常常见的,对科学应用程序非常有用。

def use_unit(unit):
"""Have a function return a Quantity with given unit"""
use_unit.ureg = pint.UnitRegistry()
def decorator_use_unit(func):
@functools.wraps(func)
def wrapper_use_unit(*args, **kwargs):
value = func(*args, *_kwargs)
return value _ use_unit.ureg(unit)
return wrapper_use_unit
return decorator_use_unit
@use_unit("meters per second")
def average_speed(distance, duration):
return distance / duration
Copy after login

8. @singledispatch

Functools 凭借非常有用的@singledispatch 装饰器再次在此列表中脱颖而出。

单调度是一种编程技术,在许多编程语言中都很常见,因为它是一种非常棒的编程方式。虽然我更喜欢多调度,但我认为单调度可以在很多方面扮演相同的角色。

这个装饰器使得在 Python 中使用多类型数据变得更加容易, 尤其当我们希望通过同一方法传递多种类型数据时,情况更是如此。

@singledispatch
def fun(arg, verbose=False):
if verbose:
print("Let me just say,", end=" ")
print(arg)
@fun.register
def _(arg: int, verbose=False):
if verbose:
print("Strength in numbers, eh?", end=" ")
print(arg)
@fun.register
def _(arg: list, verbose=False):
if verbose:
print("Enumerate this:")
for i, elem in enumerate(arg):
print(i, elem)
Copy after login

The above is the detailed content of Awesome! Recommend eight cool Python decorators!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template