python装饰器详解_python装饰器

python装饰器详解

Python comes with many syntactic artefacts that enable developers to build applications faster and most importantly, with clean code. If you’re a programmer, you know how important code quality and reliability is! So in this article, I will give a brief introduction to python decorators. If you’re interested in a cleaner and efficient coding, have a look at my following article.

Python带有许多语法伪像,使开发人员可以使用干净的代码更快,最重要的是构建应用程序。 如果您是一名程序员,您就会知道代码质量和可靠性是多么重要! 因此,在本文中,我将简要介绍python装饰器。 如果您对更简洁高效的编码感兴趣,请参阅我的以下文章。

Before we start using python decorators, we need to understand how Python functions work. Python functions are considered as first-class functions, which means they can be treated as objects and passed around at your will.

在开始使用python装饰器之前,我们需要了解Python函数的工作方式。 Python函数被认为是一流的函数 ,这意味着它们可以被视为对象并可以随意传递。

Python can have functions defined within functions, known as inner functions. A function can also be returned from another function (This is one way of implementing a switch operator in Python).

Python可以在函数内定义函数,称为内部函数 。 一个函数也可以从另一个函数返回(这是在Python中实现switch运算符的一种方式)。

函数作为OOP对象的应用 (Function’s Applications as an OOP Object)

切换案例实施 (Switch case implementation)

Python dictionary is an object construction where an object will be returned to a key that it is referred with. Since Python does not have an explicit switch operator, we use the dict construct to make one. See this example.

Python字典是一种对象构造,其中对象将返回到引用它的键。 由于Python没有显式的switch运算符,因此我们使用dict构造一个。 请参阅此示例。

op_switch = {
       
'sqr': lambda x: x**2,
'sqrt': lambda x: x**0.5,
'abs': lambda x: abs(x)
}

Our switch case is based on a string to pick the operation. The dictionary returns a function. I have used lambda function definitions for code simplicity. They behave similarly to that of functions (not exactly the same!). They can be accessed as follows.

我们的切换条件基于一个字符串来选择操作 。 字典返回一个函数。 为了简化代码,我使用了lambda函数定义。 它们的行为与功能相似(不完全相同!)。 可以按以下方式访问它们。

>>> switch['sqr'](12)
144
>>> switch['sqrt'](25)
5.0

将一个函数传递给另一个函数 (Passing a function to another function)

Consider a situation where you need to wrap another function. Imagine, the wrapper function can be shared across many other functions. Before I tell you about a real work example, let’s follow this pet scenario.

考虑一种情况,您需要包装另一个函数。 想象一下,包装器功能可以在许多其他功能之间共享。 在我告诉您一个真实的工作示例之前,让我们关注一下这种宠物场景。

Imagine you need to have a function that will either execute the function and return an answer. If an exception is thrown, None will be returned.

假设您需要一个可以执行该函数并返回答案的函数。 如果抛出一个异常, None将被退回。

def deco_function(func, *args):
try:
return func(*args)
except:
print("Error occured")
return None
def divide(a, b):
return a/b

Our deco_function function will execute the passed function with the set of args passed as *args. I have omitted keyword arguments for simplicity. If we run this, we’ll see the following output for each of the following parameters we give.

我们的deco_function函数将使用传递为*args的args集来执行传递的函数。 为了简单起见,我省略了关键字参数。 如果运行此命令,我们将看到以下给出的每个参数的输出。

>>> deco_function(divide, 10, 2)
5.0
>>> deco_function(divide, 10, 0)
Error occured

Pretty neat right!

很整洁吧!

<
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值