17

【译】PEP-3129 类装饰器

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzUyOTk2MTcwNg%3D%3D&%3Bmid=2247485006&%3Bidx=1&%3Bsn=a3f2c1f5effc023194d264484a4d4193
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

:point_up_2:  Python猫 ” ,一个值得加星标的 公众号

花下猫语: 好久没翻译文章了,但是这个系列我一直放在心上。2019 年即将完结,到新年之初,也许会有些空闲日子,我打算翻译几篇(有 PEP 也有别的)。这篇类装饰器的 PEP 很短,先开个头吧。

RBrIRfY.jpg!web

剧照 | 《大明风华》

PEP原文 : https://www.python.org/dev/peps/pep-3129

PEP标题: Class Decorators

PEP作者: Collin Winter

创建日期: 2007-05-01

合入版本: 3.0

译者 豌豆花下猫

PEP翻译计划 https://github.com/chinesehuazhou/peps-cn

摘要

本 PEP 提议推出类装饰器,它是对 PEP-318 引入的函数与方法(function and method)装饰器的扩展。

原理阐述

当初讨论函数装饰器是否该在 Python 2.4 中引入时,由于有元类,所以类装饰器被视为晦涩且不必要的[1]。但是,在使用 Python 2.4.x 系列发行版本的几年后,对函数装饰器及其使用的日益熟悉之后,BDFL 和社区重新评估了类装饰器,并建议将其包含在 Python 3.0 中[2]。

这个改变的目的是使某些构造更易于表达,并且减少对 CPython 解释器的实现细节的依赖。尽管可以使用元类来实现类似装饰器(decorator-like)功能的类,但结果通常令人不快,实现起来也很脆弱[3]。

另外,元类是要继承的,而类装饰器则不是,这使得元类不适合类装饰器的某些特定于类的使用场景。诸如 Zope 之类的大型 Python 项目正在经历这些疯狂的扭曲,就为了取得类装饰器能做到的成绩,这一点反而使 BDFL 青睐上了类装饰器。

语义

类装饰器的语义和设计目标与函数装饰器的语义和设计目标相同([4],[5]);唯一的区别是它在装饰类而不是函数。以下两个片段在语义上是相同的:

class A:
  pass
A = foo(bar(A))


@foo
@bar
class A:
  pass

有关装饰器的详细解释,请查阅 PEP-318。

实现

调整 Python 的语法以支持类修饰符,需要修改两个规则并添加一个新规则:

funcdef: [decorators] 'def' NAME parameters ['->' test] ':' suite

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
               with_stmt | funcdef | classdef

需要变成这样:

decorated: decorators (classdef | funcdef)

funcdef: 'def' NAME parameters ['->' test] ':' suite

compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt |
               with_stmt | funcdef | classdef | decorated

添加 decorated 是必要的,以避免语法出现含糊。

必须相应地修改 Python AST 和字节码。

Jack Diederich 提供了参考实现[6]。

验收

在发布此 PEP 之后,几乎没有讨论,这意味着每个人都觉得应该接受它。 补丁已提交给 Subversion,版本为 55430。

参考资料

[1] http://www.python.org/dev/peps/pep-0318/#motivation

[2] https://mail.python.org/pipermail/python-dev/2006-March/062942.html

[3] https://mail.python.org/pipermail/python-dev/2006-March/062888.html

[4] http://www.python.org/dev/peps/pep-0318/#current-syntax

[5] http://www.python.org/dev/peps/pep-0318/#design-goals

[6] https://bugs.python.org/issue1671208

版权

本文档已经放置在公共领域。源文档:

https://github.com/python/peps/blob/master/pep-3129.txt

ieYVBr2.jpg!web

感谢创作者的好文


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK