相关疑难解决方法(0)

在python中'返回的'超级对象'是什么意思?

根据http://docs.python.org/2/library/functions.html#super,

如果省略第二个参数,则返回的超级对象是未绑定的.

哪个是超级(类型).

我想知道什么是无限的,什么时候有限.

python class super

10
推荐指数
2
解决办法
1188
查看次数

为什么classmethod的超级需要第二个参数?

这按预期工作:

>>> class Foo(object):
...   @classmethod
...   def hello(cls):
...     print 'hello, foo'
... 
>>> class Bar(Foo):
...   @classmethod
...   def hello(cls):
...     print 'hello, bar'
...     super(Bar, cls).hello()
... 
>>> b = Bar()
>>> b.hello()
hello, bar
hello, foo
Run Code Online (Sandbox Code Playgroud)

我也可以显式调用基类:

>>> class Bar(Foo):
...   @classmethod
...   def hello(cls):
...     print 'hello, bar'
...     Foo.hello()
... 
>>> b = Bar()
>>> b.hello()
hello, bar
hello, foo
Run Code Online (Sandbox Code Playgroud)

我想知道为什么我不能省略第一个参数super,像这样:

>>> class Bar(Foo):
...   @classmethod
...   def hello(cls):
...     print 'hello, …
Run Code Online (Sandbox Code Playgroud)

python inheritance python-2.x class-method python-3.x

9
推荐指数
1
解决办法
977
查看次数