美文网首页
2022-04-18 装饰器

2022-04-18 装饰器

作者: 布瓜浩_albert | 来源:发表于2022-04-18 23:04 被阅读0次

Python 装饰器 概念

这个概念本身就相当地难理解。 感觉它的定义和它的名称还是有差距。

1. 首先要理解“装饰器”(decorator)

What is a Decorator?

A decorator is the name used for a software design pattern. Decorators dynamically alter the functionality of a function, method, or class without having to directly use subclasses or change the source code of the function being decorated.

装饰器是一种软件设计模式,在不用子类或者修改原代码的前提下,自动地改变函数、方法或者类的功能。

2. 理解Python 装饰器

What is a Python Decorator?

The "decorators" we talk about with concern to Python are not exactly the same thing as the DecoratorPattern described above. A Python decorator is a specific change to the Python syntax that allows us to more conveniently alter functions and methods (and possibly classes in a future version).

Python 装饰器 与上面提到的装饰器模式还是有差别的。Python 装饰器其实是一个很特别语法修改,让我们更方便地改变函数或者方法(在未来的版本中,有可能包含修改类)。

装饰器有什么用?

装饰器有什么用,或者说我们在什么场景下需要不改变原函数或方法的代码,不使用子类,而又要改变函数或方法的功能呢?

由于我不是大神,所以只能从网上捞一些大神的答复。我是被说服了。总体来说,就几个点:

1) 方便套在其他函数上面

2) 不用重复写同样功能的代码

3) 临时性的添加一些功能,又不修改原代码,比如测试或者debug时

4) 增加代码的易读性

Because then for each combination of the decorator functions you have you will have to write another decorator function. Compositing decorator functions are the abstraction so you don't have to manually write those combinations yourself. – Netwave Oct 1, 2018 at 14:47

Package a decorator (or multiple), and you can easily apply all that functionality to other packages, without having to write new functions in those other packages: just one extra line for the relevant functions. – 9769953 Oct 1, 2018 at 14:49

And yes, technically you can precisely do what you suggest. But it makes things more cumbersome, and pollutes the code with extra functions. A decorator with clear functionality (from its name) makes code more readable, instead of having to read one function, only to then look up the other function. – 9769953 Oct 1, 2018 at 14:50 1

I'm not exactly an expert on design patterns, but I think Python decorators are not the same as the decorator pattern. (Maybe they are related at an abstract level, but the dup-targets do not seem to be practically relevant for this question.) – MB-F Oct 1, 2018 at 14:58

Decorators are very common when the function modification is temporary. Consider the use of decorators in adding testing or timing functions. – Michael Molter Oct 1, 2018 at 15:01

如何使用装饰器

这个问题,我找了很多网上的资料,以下这个比较容易懂。

https://pythonbasics.org/decorators/

相关文章

  • 2022-04-18 装饰器

    Python 装饰器 概念 这个概念本身就相当地难理解。 感觉它的定义和它的名称还是有差距。 1. 首先要理解“装...

  • 装饰器

    """@装饰器- 普通装饰器- 带参数的装饰器- 通用装饰器- 装饰器装饰类- 内置装饰器- 缓存装饰器- 类实现...

  • typescript 五种装饰器

    装饰器类型 装饰器的类型有:类装饰器、访问器装饰器、属性装饰器、方法装饰器、参数装饰器,但是没有函数装饰器(fun...

  • python——装饰器详解

    一、装饰器概念 1、装饰器 装饰器:一种返回值也是一个函数的函数,即装饰器。 2、装饰器目的 装饰器的目的:装饰器...

  • Python装饰器

    Python装饰器 一、函数装饰器 1.无参装饰器 示例:日志记录装饰器 2.带参装饰器 示例: 二、类装饰器 示例:

  • Python中的装饰器

    Python中的装饰器 不带参数的装饰器 带参数的装饰器 类装饰器 functools.wraps 使用装饰器极大...

  • 装饰器

    装饰器 decorator类装饰器 带参数的装饰器 举例(装饰器函数;装饰器类;有参与无参) https://fo...

  • TypeScript装饰器

    前言 装饰器分类 类装饰器 属性装饰器 方法装饰器 参数装饰器需要在tsconfig.json中启用experim...

  • python之装饰器模版

    装饰器的作用:装饰器即可以装饰函数也可以装饰类。装饰器的原理:函数也是对象 1.定义装饰器 2.使用装饰器假设de...

  • 装饰器实验

    装饰器实验 说明 ts内包含了四个装饰器,类装饰器、属性装饰器、函数装饰器、参数装饰器,本文中测试一下其的使用。 ...

网友评论

      本文标题:2022-04-18 装饰器

      本文链接:https://www.haomeiwen.com/subject/rxdjertx.html