Python小姿势 - import random

简介: 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.


相关文章
|
3天前
|
资源调度 算法 JavaScript
Python基础专题 - 超级详细的 Random(随机)原理解析与编程实践
Python基础专题 - 超级详细的 Random(随机)原理解析与编程实践
157 0
|
3天前
|
数据可视化 数据挖掘 开发者
import有什么用,python中怎么使用import
import有什么用,python中怎么使用import
29 1
|
5月前
|
API Python
python 详细理解 import ---- openstack自动import class 到 特定命名空间
python 详细理解 import ---- openstack自动import class 到 特定命名空间
53 0
|
6月前
|
Python
89 python高级 - import导入模块
89 python高级 - import导入模块
27 0
|
7月前
|
XML Shell Linux
python内置模块(random、os、sys、shelve、configparser、xml)
python内置模块(random、os、sys、shelve、configparser、xml)
46 0
|
6月前
|
算法 安全 量子技术
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
138 0
|
3天前
|
资源调度 算法 Python
「Python系列」Python random模块、hashlib模块
`random` 模块在 Python 中提供了多种生成随机数的方法
32 0
|
3天前
|
Python
Python random模块(获取随机数)常用方法和使用例子
`random`模块在Python中用于生成随机数。
24 0
|
3天前
|
资源调度 Python
|
3天前
|
Python
[Python] 如何用import导入模块
[Python] 如何用import导入模块
27 0