Advanced Functions and Decorators in Python

1. Pre-Learning Highlights

We have previously learned about functions in Python. If we say that the Python language is vast and feature-rich, what are the core elements? I believe that besides basic data types and control structures like conditionals and loops, functions and classes are the most essential components. Mastering these two skills allows you to write various applications.

For beginners, using functions may just mean printing “hello, world”. However, as we delve deeper, we will discover that Python functions are incredibly powerful! For instance, what is a higher-order function? We have learned about function return values; what does it mean if a return statement returns a function name?

We can think of it this way: any function that has a function name in its return statement is a higher-order function. So, what is the use of higher-order functions? The most typical application is to implement decorators.

So, what exactly are decorators used for? Let’s begin our formal study below.

2. Higher-Order Functions and Decorators in Python

Decorators are an advanced feature in Python that allows you to dynamically modify the behavior of functions or classes. A decorator is a function that takes another function as an argument and returns a new function or modifies the original function.

Python decorators = syntax sugar that dynamically adds new functionality to functions without modifying the original function code or changing the way they are called.

Core Idea: Higher-order functions + Closures + Syntax sugar @.

2.1 Why Use Decorators and Their Application Scenarios

Code Reusability: One piece of logic (timing, permission checks, caching, logging) can be applied to N functions.

Adhering to the “Open-Closed Principle”: Open for extension, closed for modification.

High Readability: @timer immediately indicates “this function is being timed”.

Typical Application Scenarios for Decorators:

Logging: Decorators can be used to log function call information, parameters, and return values.

Performance Analysis: Decorators can be used to measure the execution time of functions.

Access Control: Decorators can be used to restrict access to certain functions.

Caching: Decorators can be used to implement caching of function results to improve performance.

2.2 Minimal Example of Using a Decorator (Handwritten Timing Decorator)

Advanced Functions and Decorators in Python

Execution Result:

Advanced Functions and Decorators in Python

Explanation:

slow_add is wrapped by @timer, and what is actually received is the wrapper.

The caller still writes slow_add(…), without needing to know that timing logic exists.

2.3 Decorators with Parameters (Three-Level Nesting)

Advanced Functions and Decorators in Python

Output Result:

Advanced Functions and Decorators in Python

2.4 Common Built-in “Higher-Order Decorators” in the System

Decorator

Function

Example

<span>@lru_cache(maxsize=128)</span>

Automatically caches function return values

Accelerating recursive Fibonacci

<span>@staticmethod</span>

Converts a method to a static function

Class utility function

<span>@property</span>

Turns a method into a property

Implementing getter/setter

3.

Summary

In summary, the role of decorators can be expressed in one sentence:

Decorators = Using higher-order functions to wrap functions; write once, reuse everywhere with @, clean and elegant. Let’s maintain our enthusiasm for learning and practice more. See you next time!

Advanced Functions and Decorators in Python

#python

Leave a Comment