相关疑难解决方法(0)

super()失败并出现错误:当父不从对象继承时,TypeError"参数1必须是type,而不是classobj"

我得到一些我无法弄清楚的错误.任何线索我的示例代码有什么问题?

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 inheritance object parent super

186
推荐指数
4
解决办法
11万
查看次数

如何初始化基础(超级)类?

在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上搜索时,我找到了不止一种方法.处理这个问题的标准方法是什么?

python oop

116
推荐指数
4
解决办法
14万
查看次数

标签 统计

python ×2

inheritance ×1

object ×1

oop ×1

parent ×1

super ×1