I tried to inherit with Python and init with super, but TypeError: must be type, not classobj Memories when it was hard to say
First, the Python version
shell
$ python -V
Python 2.7.10
I was trying to create another class by inheriting the following class. Inheritance source class
hogehoge.py
class Client():
def __init__(self, url, **kwargs):
Inheritance class
manager.py
import hogehoge
class Manager(hogehoge.Client):
def __init__(self, url, *kwargs):
super(Manager,self).__init__(url, *kwargs)
Try using
shell
$ python
>>> import manager
>>> cl = manager.Manager("https://example.jp")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "manager.py", line 10, in __init__
super(Manager,self).__init__(url, *kwargs)
TypeError: must be type, not classobj
I didn't understand even if I googled.
The stackoverflow that I finally arrived at is below.
python - super() raises "TypeError: must be type, not classobj" for new-style class - Stack Overflow http://stackoverflow.com/questions/9698614/super-raises-typeerror-must-be-type-not-classobj-for-new-style-class
?? ?? Are there new and old objects? ?? See below for the relationship between Python types and classes
Relationship between python types and classes — Paisonnikki http://pydiary.bitbucket.org/blog/html/2013/10/12/type.html
There are two types of classes in Python ~~ objects ~~.
The instance created by hogehoge.py above is an old classobj and not a type, so it can't be super. It seems to be an error.
The solution is below. ~~ Read the parent class as an object in the inherited class ~~ Multiple inheritance of both Client and object Thank you @shiracamus.
manager.py
import hogehoge
class Manager(hogehoge.Client, object):
def __init__(self, url, *kwargs):
super(Manager,self).__init__(url, *kwargs)
I'm new to Python, so please point out any mistakes.
Recommended Posts