Python小姿势 – import random

import random

topic = random.choice([‘python decorator’, ‘python generator’, ‘python yield’, ‘python list comprehension’])

print(‘How to use {} in Python?’.format(topic))

If you’re a Python programmer, then you’ve probably already used functions like len(), print(), or range(). But did you know that these are actually just “wrapper” functions that make use of other more powerful functions?

In Python, these wrapper functions are called decorators. Decorators allow you to “wrap” a function in another function, and modify the behavior of the wrapped function without actually changing the code of the function itself.

In this article, we’ll take a look at how to use decorators in Python, and see some examples of how they can be used to make your code more concise and easier to read.

So what exactly is a decorator?

A decorator is a function that takes another function as an argument, and returns a new function that “wraps” the original function.

For example, let’s say we have a function that prints a message to the console:

def print_message(message): print(message)

Now, let’s say we want to add some extra behavior to this function, like logging the message to a file. We could do this by defining a new function that calls the print_message() function and also logs the message to a file:

def log_message(message): print_message(message) with open(‘log.txt’, ‘a’) as f: f.write(message + ‘\n’)

But this approach has a couple of problems. First, it requires us to duplicate the code of the print_message() function in the log_message() function. Second, it means that if we ever want to change the behavior of the print_message() function, we have to change the code in two places (in the print_message() function and in the log_message() function).

Fortunately, there’s a better way to do this using decorators. We can define a decorator function that takes the print_message() function as an argument and returns a new function that “wraps” the print_message() function:

def log_decorator(func): def wrapper(message): print_message(message) with open(‘log.txt’, ‘a’) as f: f.write(message + ‘\n’) return wrapper

Now we can use this decorator to “wrap” the print_message() function:

@log_decorator def print_message(message): print(message)

When we use the @log_decorator decorator, it’s equivalent to calling the log_decorator() function with the print_message() function as an argument, and then assigning the return value (the new “wrapped” function) to the print_message() function.

So what happens when we call the print_message() function now?

print_message(‘Hello, world!’)

Hello, world!

The output of the above code is the same as if we had called the log_message() function:

Hello, world!

How do decorators work?

The key to understanding how decorators work is to understand that functions are first-class objects in Python. This means that they can be passed as arguments to other functions, and they can be returned by other functions.

When we use the @log_decorator decorator, what actually happens is that the print_message() function is passed as an argument to the log_decorator() function, and the return value (the new “wrapped” function) is assigned to the print_message() function.

Let’s take a look at a slightly simplified version of the log_decorator() function to see how this works:

def log_decorator(func): def wrapper(message): print_message(message) with open(‘log.txt’, ‘a’) as f: f.write(message + ‘\n’) return wrapper

First, we define a function that takes a function as an argument. Then, we define a wrapper function that calls the func() function (which is the print_message() function in our case) and also logs the message to a file. Finally, we return the wrapper function.

When we use the @log_decorator decorator, the print_message() function is passed as an argument to the log_decorator() function, and the return value (the wrapper function) is assigned to the print_message() function.

顺便介绍一下我的另一篇专栏, 《100天精通Python – 快速入门到黑科技》专栏,是由 CSDN 内容合伙人丨全站排名 Top 4 的硬核博主 不吃西红柿 倾力打造。 基础知识篇以理论知识为主,旨在帮助没有语言基础的小伙伴,学习我整理成体系的精华知识,快速入门构建起知识框架;黑科技应用篇以实战为主,包括办公效率小工具、爬虫、数据分析、机器学习、计算机视觉、自然语言处理、数据可视化等等,让你会用一段简单的Python程序,自动化解决工作和生活中的问题,甚至成为红客。

🍅 订阅福利原价299,限时1折订阅专栏进入千人全栈VIP答疑群,作者优先解答机会(代码指导/学习方法指引),群里大佬可以抱团取暖(大厂/外企内推机会)

🍅 订阅福利简历指导、招聘内推、80G全栈学习视频、300本IT电子书:Python、Java、前端、大数据、数据库、算法、爬虫、数据分析、机器学习、面试题库等等

🍅 专栏地址: 点击《100天精通Python – 快速入门到黑科技》

100天精通Python - 订阅福利

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年5月23日
下一篇 2023年5月23日

相关推荐