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

时间:2023-01-28 17:03:03

I get some error that I can't figure out. Any clue what is wrong with my sample code?

我得到了一些我不知道的错误。我的示例代码有什么问题吗?

class B:
    def meth(self, arg):
        print arg

class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)

I got the sample test code from help of 'super' built-in method. The class "C" is the

我从“super”内置方法中获得了示例测试代码。类“C”是

Here is the error:

这是错误:

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

FYI, here is the help(super) from python itself:

这里是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 super object; requires issubclass(type2, type)
 |  Typical use to call a cooperative superclass method:
 |  class C(B):
 |      def meth(self, arg):
 |          super(C, self).meth(arg)
 |

4 个解决方案

#1


218  

Your problem is that class B is not declared as a "new-style" class. Change it like so:

你的问题是B类没有被声明为“新风格”类。改变它像这样:

class B(object):

and it will work.

它会工作。

super() and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that (object) on any class definition to make sure it is a new-style class.

super()和所有子类/超类都只适用于新式类。我建议您养成在任何类定义上输入那个(对象)的习惯,以确保它是一个新样式的类。

Old-style classes (also known as "classic" classes) are always of type classobj; new-style classes are of type type. This is why you got the error message you saw:

旧式类(也称为“经典”类)总是类型为classobj;新样式类是类型类型的。这就是为什么你看到了错误信息:

TypeError: super() argument 1 must be type, not classobj

TypeError: super()参数1必须是type,而不是classobj

Try this to see for yourself:

你自己试试这个:

class OldStyle:
    pass

class NewStyle(object):
    pass

print type(OldStyle)  # prints: <type 'classobj'>

print type(NewStyle) # prints <type 'type'>

Note that in Python 3.x, all classes are new-style. You can still use the syntax from the old-style classes but you get a new-style class. So, in Python 3.x you won't have this problem.

注意,在Python 3中。x,所有的类都是新型的。您仍然可以使用来自旧式类的语法,但是您会得到一个新的类。所以,在Python 3。你不会有这个问题。

#2


100  

Also, if you can't change class B, you can fix the error by using multiple inheritance.

另外,如果不能更改类B,可以使用多重继承来修复错误。

class B:
    def meth(self, arg):
        print arg

class C(B, object):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)

#3


11  

If the python version is 3.X, it's okay.

如果python版本是3。X,没关系。

I think your python version is 2.X, the super would work when adding this code

我认为您的python版本是2。当添加这个代码时,super将会工作。

__metaclass__ = type

so the code is

代码是

__metaclass__ = type
class B:
    def meth(self, arg):
        print arg
class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)
print C().meth(1)

#4


2  

I was also faced by the posted issue when I used python 2.7. It is working very fine with python 3.4

我在使用python 2.7时也遇到了发布的问题。它在python 3.4中运行得非常好

To make it work in python 2.7 I have added the __metaclass__ = type attribute at the top of my program and it worked.

为了使它在python 2.7中工作,我在程序的顶部添加了__metaclass__ = type属性,它是有效的。

__metaclass__ : It eases the transition from old-style classes and new-style classes.

__metaclass__:它简化了从旧式类到新式类的转换。

#1


218  

Your problem is that class B is not declared as a "new-style" class. Change it like so:

你的问题是B类没有被声明为“新风格”类。改变它像这样:

class B(object):

and it will work.

它会工作。

super() and all subclass/superclass stuff only works with new-style classes. I recommend you get in the habit of always typing that (object) on any class definition to make sure it is a new-style class.

super()和所有子类/超类都只适用于新式类。我建议您养成在任何类定义上输入那个(对象)的习惯,以确保它是一个新样式的类。

Old-style classes (also known as "classic" classes) are always of type classobj; new-style classes are of type type. This is why you got the error message you saw:

旧式类(也称为“经典”类)总是类型为classobj;新样式类是类型类型的。这就是为什么你看到了错误信息:

TypeError: super() argument 1 must be type, not classobj

TypeError: super()参数1必须是type,而不是classobj

Try this to see for yourself:

你自己试试这个:

class OldStyle:
    pass

class NewStyle(object):
    pass

print type(OldStyle)  # prints: <type 'classobj'>

print type(NewStyle) # prints <type 'type'>

Note that in Python 3.x, all classes are new-style. You can still use the syntax from the old-style classes but you get a new-style class. So, in Python 3.x you won't have this problem.

注意,在Python 3中。x,所有的类都是新型的。您仍然可以使用来自旧式类的语法,但是您会得到一个新的类。所以,在Python 3。你不会有这个问题。

#2


100  

Also, if you can't change class B, you can fix the error by using multiple inheritance.

另外,如果不能更改类B,可以使用多重继承来修复错误。

class B:
    def meth(self, arg):
        print arg

class C(B, object):
    def meth(self, arg):
        super(C, self).meth(arg)

print C().meth(1)

#3


11  

If the python version is 3.X, it's okay.

如果python版本是3。X,没关系。

I think your python version is 2.X, the super would work when adding this code

我认为您的python版本是2。当添加这个代码时,super将会工作。

__metaclass__ = type

so the code is

代码是

__metaclass__ = type
class B:
    def meth(self, arg):
        print arg
class C(B):
    def meth(self, arg):
        super(C, self).meth(arg)
print C().meth(1)

#4


2  

I was also faced by the posted issue when I used python 2.7. It is working very fine with python 3.4

我在使用python 2.7时也遇到了发布的问题。它在python 3.4中运行得非常好

To make it work in python 2.7 I have added the __metaclass__ = type attribute at the top of my program and it worked.

为了使它在python 2.7中工作,我在程序的顶部添加了__metaclass__ = type属性,它是有效的。

__metaclass__ : It eases the transition from old-style classes and new-style classes.

__metaclass__:它简化了从旧式类到新式类的转换。