'what is the correct way to inherit class in python [closed]

I have a base class as follows:

class BaseSqlClass:
    def __init__(self):
        self.__init__()

    @staticmethod
    def some_func(env, db):
        # do something

I have a child class which inherits from base class as follows:

class SqlGenerator(BaseSqlClass):
    def __init__(self, env, db, table):
        super().__init__()
        self.env = env
        self.db = db
        self.table = table

When i instantiate the child class, I get the error __init__() missing positional arguments, __init__() is of base class? I don't know what I am doing wrong.



Solution 1:[1]

The error you posted does not fit to the code you posted, so I'll go with the code. First, you should be getting an error maximum recursion depth exceeded for

class BaseSqlClass:
    def __init__(self):
        self.__init__()

    @staticmethod
    def some_func(env, db):
        # do something

When you create a BaseSqlClass instance, the __init__ method is called, from which you call that same method again, which calls itself again, and so on. If you don't want to do anything at instance creation time, just omit the __init__ method:

class BaseSqlClass:
    @staticmethod
    def some_func(env, db):
        # do something

Second issue is in the second code snippet - you are calling some __init method of the parent class - but that doesn't have such a method. I guess you meant super().__init__() instead of super().__init() ?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Programmer