search
HomeBackend DevelopmentPython TutorialAwesome! Recommend eight cool Python decorators!

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

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

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

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)

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()

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)

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):

另一种方法是使用元类!

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

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)

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!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

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 Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.