相关疑难解决方法(0)

对于新式类,super()引发"TypeError:必须是type,而不是classobj"

以下使用super()引发了一个TypeError:为什么?

>>> from  HTMLParser import HTMLParser
>>> class TextParser(HTMLParser):
...     def __init__(self):
...         super(TextParser, self).__init__()
...         self.all_data = []
...         
>>> TextParser()
(...)
TypeError: must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

StackOverflow上有一个类似的问题:Python super()引发TypeError,其中错误的解释是用户类不是新式类.但是,上面的类是一个新式的类,因为它继承自object:

>>> isinstance(HTMLParser(), object)
True
Run Code Online (Sandbox Code Playgroud)

我错过了什么?我怎么用super(),在这里?

使用HTMLParser.__init__(self)而不是super(TextParser, self).__init__()工作,但我想了解TypeError.

PS:Joachim指出,作为一个新式的实例并不等同于一个object.我多次反复阅读,因此我的困惑(基于object实例测试的新式类实例测试示例:https://stackoverflow.com/revisions/2655651/3).

python super typeerror superclass

331
推荐指数
6
解决办法
13万
查看次数

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 2.x陷阱和地雷

我的问题的目的是通过Python加强我的知识库并更好地了解它,包括了解它的错误和意外.为了保持特定的内容,我只对CPython解释器感兴趣.

我正在寻找类似于从我的PHP地雷 问题中学到的东西,其中一些答案对我来说是众所周知的但是一对夫妇的边界恐怖.

更新:显然有一两个人感到不安,我问了一个已经在Stack Overflow之外部分回答的问题.这里的某些妥协是URL http://www.ferg.org/projects/python_gotchas.html

请注意,此处的一个或两个答案已经是上面引用的网站上的原始答案.

python

61
推荐指数
16
解决办法
1万
查看次数

Python:需要参数时使用super()调用基类的__init __()方法

我试图__init__()在一个超类中调用该方法,其中所说的方法接受参数,但是似乎不起作用。请参见下面的代码:

>>> class A:
        def __init__(self, param1, param2):
            self._var1 = param1
            self._var2 = param2

>>> class B(A):
        def __init__(self, param1, param2, param3):
            super(B, self).__init__(param1, param2)
            self._var3 = param3


>>> a = A("Hi", "Bob")
>>> a._var1
'Hi'
>>> a._var2
'Bob'
>>> 
>>> b = B("Hello", "There", "Bob")

Traceback (most recent call last):
  File "<pyshell#74>", line 1, in <module>
    b = B("Hello", "There", "Bob")
  File "<pyshell#69>", line 3, in __init__
    super(B, self).__init__(param1, param2)
TypeError: must be type, not classobj
>>> …
Run Code Online (Sandbox Code Playgroud)

python inheritance super

4
推荐指数
1
解决办法
1211
查看次数

如何在Python 2.7中使用带有参数的super()?

我问这个问题,因为关于这个主题的每一个其他页面似乎都转变为过于简单的案例或对范围之外的细节进行随机讨论.

class Base:
    def __init__(self, arg1, arg2, arg3, arg4):
        self.arg1 = arg1
        self.arg2 = arg2
        self.arg3 = arg3
        self.arg4 = arg4

class Child(Base):
    def __init__(self, arg1, arg2, arg3, arg4, arg5):
        super(Child).__init__(arg1, arg2, arg3, arg4)
        self.arg5 = arg5
Run Code Online (Sandbox Code Playgroud)

这就是我尝试过的.我被告知我需要使用类型而不是classobj,但我不知道这意味着什么,谷歌没有帮助.

我只是想让它发挥作用.在Python 2.7中这样做的正确做法是什么?

python arguments class super python-2.7

2
推荐指数
1
解决办法
3043
查看次数

在Python2.7中使用'super'的示例

我试图在python中使用super扩展我的类初始化.在下面的例子中,我想Class A初始化一个参数乘以4,我希望这个参数可以通过继承在B类中使用.然后我想在B中进行另一次初始化以获取相同的参数并将其乘以8.第二次初始化不必在A中可用.

class A():
    def __init__(self,parameter):
        self.initialize_parameter=4*parameter

class B(A):
    def __init__(self,parameter): #note this parameter is intended to be the same parameter from Class A
        self.another_parameter=parameter*8
        super(B,self).__init__(parameter)
Run Code Online (Sandbox Code Playgroud)

我一直在阅读的文件似乎表明以上是正确的语法(我认为),但关于这个主题的文档相当混乱.上面的代码返回TypeError: must be type, not classobj.

这是我第一次使用继承,有人可以建议我做错了吗?

python inheritance super

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