我得到一些我无法弄清楚的错误.任何线索我的示例代码有什么问题?
class B:
def meth(self, arg):
print arg
class C(B):
def meth(self, arg):
super(C, self).meth(arg)
print C().meth(1)
Run Code Online (Sandbox Code Playgroud)
我从"超级"内置方法的帮助下得到了示例测试代码."C"级是
这是错误:
Traceback (most recent call last):
File "./test.py", line 10, in ?
print C().meth(1)
File "./test.py", line 8, in meth
super(C, self).meth(arg)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)
仅供参考,这是来自python本身的帮助(超级):
Help on class super in module __builtin__:
class super(object)
| super(type) -> unbound super object
| super(type, obj) -> bound super object; requires isinstance(obj, type)
| super(type, type2) -> bound …Run Code Online (Sandbox Code Playgroud) 在Python中,请考虑我有以下代码:
>>> class SuperClass(object):
def __init__(self, x):
self.x = x
>>> class SubClass(SuperClass):
def __init__(self, y):
self.y = y
# how do I initialize the SuperClass __init__ here?
Run Code Online (Sandbox Code Playgroud)
如何SuperClass __init__在子类中初始化?我正在关注Python教程,但它没有涵盖这一点.当我在Google上搜索时,我找到了不止一种方法.处理这个问题的标准方法是什么?