Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python的super函数是否需要参数

$
0
0

作者:杨冬 欢迎转载,也请保留这段声明。谢谢!

出处: https://andyyoung01.github.io/ 或 http://andyyoung01.16mb.com/

在浏览开源社区的代码时,有时候会看到在调用super函数时有的有参数,有的没有参数。其实这只是python2和Python3不同的语法而已。Python3允许使用不带参数的super函数,而Python2的super函数中需要带有参数。

在Python3的 官方文档 中也说明了这一点。拿文档中的例子来看:

class C(B): def method(self, arg): super().method(arg) # This does the same thing as: # super(C, self).method(arg)

如果是Python2的话,必须使用 super(C, self).method(arg) 这种语法。


Viewing all articles
Browse latest Browse all 9596

Trending Articles