为什么在使用super()时必须指定自己的类,并且有一种解决方法? [英] Why do I have to specify my own class when using super(), and is there a way to get around it?
本文介绍了为什么在使用super()时必须指定自己的类,并且有一种解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Python的super()
进行方法链接时,必须显式指定自己的类,例如:
When using Python's super()
to do method chaining, you have to explicitly specify your own class, for example:
class MyDecorator(Decorator):
def decorate(self):
super(MyDecorator, self).decorate()
我必须指定类MyDecorator
的名称作为super()
的参数.这不是DRY.现在,当我重命名我的班级时,我将不得不重命名它两次.为什么以这种方式实施?有没有一种方法可以避免不得不两次(或多次)写入类的名称?
I have to specify the name of my class MyDecorator
as an argument to super()
. This is not DRY. When I rename my class now I will have to rename it twice. Why is this implemented this way? And is there a way to weasel out of having to write the name of the class twice(or more)?
推荐答案
BDFL同意.参见2.6的 Pep 367-新超级和
The BDFL agrees. See Pep 367 - New Super for 2.6 and PEP 3135 - New Super for 3.0.
这篇关于为什么在使用super()时必须指定自己的类,并且有一种解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文