It was a painful memory when I was told TypeError: must be type, not class obj when trying to inherit with Python.

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 ~~.

  1. classobj
  2. type

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

It was a painful memory when I was told TypeError: must be type, not class obj when trying to inherit with Python.
I get a UnicodeDecodeError when trying to connect to oracle with python sqlalchemy
When I tried to create a virtual environment with Python, it didn't work
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
[python] A note when trying to use numpy with Cython
A memorandum when I tried to get it automatically with selenium
A note I was addicted to when creating a table with SQLAlchemy
[Python] Inherit a class with class variables
[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()
When I tried to run Python, it was skipped to the Microsoft Store
A story I was addicted to when inserting from Python to a PostgreSQL table
A story I was addicted to trying to get a video url with tweepy
Use Python from Java with Jython. I was also addicted to it.
[Python / Pandas] A bug occurs when trying to replace a DataFrame with `None` with` replace`
[Python] It was very convenient to use a Python class for a ROS program.
I was addicted to trying Cython with PyCharm, so make a note
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
[Python] How to deal with the is instance error "is instance () arg 2 must be a type or tuple of types"
When I made a treemap (area graph) with python, it was subtle, so when I used flourish, it felt pretty good.
When I tried to make a VPC with AWS CDK but couldn't make it
What to do when "TypeError: must be string, not int…" appears when using strptime
Three things I was addicted to when using Python and MySQL with Docker
I was able to mock AWS-Batch with python, moto, so I will leave it
Python 3 migration> TypeError: must be real number, not map support> change map () to list (map ())
When I tried to create a project using Python on Docker with PyCharm, it didn't work, but it worked with Docker Compose.
I want to write to a file with Python
I tried to automatically generate a character string to be input to Mr. Adjustment with Python
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
What I did when I was angry to put it in with the enable-shared option
A memo of misunderstanding when trying to load the entire self-made module with Python3
A beginner tried coloring line art with chainer. I was able to do it.
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
[Go language] Be careful when creating a server with mux + cors + alice. Especially about what I was addicted to around CORS.
When writing to a csv file with python, a story that I made a mistake and did not meet the delivery date
I wrote a class that makes it easier to divide by specifying part of speech when using Mecab in python
I tried to draw a route map with Python
I was addicted to scraping with Selenium (+ Python) in 2020
I want to inherit to the back with python dataclass
I want to work with a robot in python.
I tried to automatically generate a password with Python3
I was able to repeat it in Python: lambda
What I was addicted to when using Python tornado
I want to run a quantum computer with Python
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
I made a class to get the analysis result by MeCab in ndarray with python
When creating a pipenv environment, I got addicted to "Value Error: Not a valid python path"
I was surprised to receive a nice review when I wrote Python to CheckIO and its explanation
What I was addicted to when dealing with huge files in a Linux 32bit environment
Since there was a doppelganger, I tried to distinguish it with artificial intelligence (laugh) (Part 2)
I made a server with Python socket and ssl and tried to access it from a browser
[Note] A story about trying to override a class method with two underscores in Python 3 series.
I tried to make a generator that generates a C # container class from CSV with Python
A program that failed when trying to create a linebot with reference to "Dialogue system made with python"
There was a doppelganger, so I tried to distinguish it with artificial intelligence (laughs) (Part 1)
I want to output while converting the value of the type (e.g. datetime) that is not supported when outputting json with python