Super() Fails with Error: Typeerror "Argument 1 Must Be Type, Not Classobj" When Parent Does Not Inherit from Object

super() fails with error: TypeError argument 1 must be type, not classobj when parent does not inherit from object

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

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.

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:

TypeError: super() argument 1 must be type, not 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 super() argument 1 must be type, not classobj

No, you did use super correctly. The first line:

class foo():

should be

class foo(object):

The reason for this is that your class is not a "new-style" class. super() only works on new style classes



Reference: https://www.programiz.com/python-programming/methods/built-in/super

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

Frame is not a new-style class, but super requires new-style classes to work. In python-3.x where everything is a new-style class the super will work properly.

You need to hardcode the superclass and method in python 2:

Frame.__init__(self, master)

Like they do in the official documentation.

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

You should do instead of super(chat_id, user1).__init__() you should do:

super().__init__(chat_id, user1) # Work in Python 3.6
super(GroupMessage, self).__init__(chat_id, user1) # Work in Python 2.7

or

Chats.__init__(self, chat_id, user1)

This last option is not recommended if exist change that your class hierarchy change in the future. I really don't like it for others motive but still worth it a mention.

super() raises TypeError: must be type, not classobj for new-style class

Alright, it's the usual "super() cannot be used with an old-style class".

However, the important point is that the correct test for "is this a new-style instance (i.e. object)?" is

>>> class OldStyle: pass
>>> instance = OldStyle()
>>> issubclass(instance.__class__, object)
False

and not (as in the question):

>>> isinstance(instance, object)
True

For classes, the correct "is this a new-style class" test is:

>>> issubclass(OldStyle, object)  # OldStyle is not a new-style class
False
>>> issubclass(int, object) # int is a new-style class
True

The crucial point is that with old-style classes, the class of an instance and its type are distinct. Here, OldStyle().__class__ is OldStyle, which does not inherit from object, while type(OldStyle()) is the instance type, which does inherit from object. Basically, an old-style class just creates objects of type instance (whereas a new-style class creates objects whose type is the class itself). This is probably why the instance OldStyle() is an object: its type() inherits from object (the fact that its class does not inherit from object does not count: old-style classes merely construct new objects of type instance). Partial reference: https://stackoverflow.com/a/9699961/42973.

PS: The difference between a new-style class and an old-style one can also be seen with:

>>> type(OldStyle)  # OldStyle creates objects but is not itself a type
classobj
>>> isinstance(OldStyle, type)
False
>>> type(int) # A new-style class is a type
type

(old-style classes are not types, so they cannot be the type of their instances).

Python super() raises TypeError

The reason is that super() only operates on new-style classes, which in the 2.x series means extending from object:

>>> class X(object):
def a(self):
print 'a'

>>> class Y(X):
def a(self):
super(Y, self).a()
print 'b'

>>> c = Y()
>>> c.a()
a
b

Django problem super() argument 1 must be type, not WSGIRequest in Python 3

You have misunderstood how to use super(). You'd pass in the current class and an instance or class for the second argument, not the request object. The result of that call is a special object that knows how to find and bind attributes on the parent classes by disregarding the current class.

In a staticmethod context, you would have to pass in the current class as both arguments:

class Estatisticas(DatasetRequired):
@staticmethod
def get(request):
super(Estatisticas, Estatisticas).get(request)
# <do various other stuff>

I'm really not sure why you are using a staticmethod here. When handling a request, a special instance is created for a view, so you normally use normal instance methods. At that point, in Python 3, you can use super() with no arguments:

class DatasetRequired(View):

def get(self, request):
# <redirects the user>

class Estatisticas(DatasetRequired):

def get(self, request):
super().get(request)
# <do various other stuff>

Python has enough context to know that super() needs Estatisticas and self as arguments without you having to name those.



Related Topics



Leave a reply



Submit