superclass

how to use super().__init__() method for 2 or more classes?

how to use super().__init__() method for 2 or more classes? Question: Here I have provided an example to illustrate my problem when a class inherits from 2 classes, then the super method only calls the constructor of the first inherited class. import inspect class A : def __init__(self): self.atr1 = ‘Foo1’ class B : def …

Total answers: 1

Why use a superclass's __init__ to change it into a subclass?

Why use a superclass's __init__ to change it into a subclass? Question: I’m working on replicating the SHAP package algorithm – an explainability algorithm for machine learning. I’ve been reading through the author’s code, and I’ve come across a pattern I’ve never seen before. The author has created a superclass called Explainer, which is a …

Total answers: 1

The Hit Object Within the BlockLogic Superclass is Not Being Called

The Hit Object Within the BlockLogic Superclass is Not Being Called Question: When I press the lowercase L key, I want the hit object to be called, reducing the length and width of the squares by 5 each time. It seems that the code within BlockNorm is not referencing the hit object within BlockLogic properly. …

Total answers: 1

Updating Old wxPython App: Invalid Window Calling Super/Parent Method

Updating Old wxPython App: Invalid Window Calling Super/Parent Method Question: I am trying to update an old app that was written for Python 2.7 with wxWidgets/wxPython 2.8. I am trying to make it compatible with my current system’s versions of Python (3.10) & wxPython (4.0). I have come across an error trying to call a …

Total answers: 2

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

TypeError: super() argument 1 must be type, not int (Python) Question: I am trying to create a GroupMessage class that inherits some of its attributes from the main Chat class. I am getting an error at the super(chat_id, user1).__init__() line. I am unable to fix it! class Chats(object): def __init__(self, chat_id, user1): self.id = chat_id …

Total answers: 1

Difference between super() and calling superclass directly

Difference between super() and calling superclass directly Question: In Python 2.7 and 3, I use the following method to call a super-class’s function: class C(B): def __init__(self): B.__init__(self) I see it’s also possible to replace B.__init__(self) with super(B, self).__init__() and in python3 super().__init__(). Are there any advantages or disadvantages to doing this either way? It …

Total answers: 1

Python super() behavior not dependable

Python super() behavior not dependable Question: For some reason, the super() method is not always behaving as expected, opting to return: TypeError(‘super(type, obj): obj must be an instance or subtype of type)’ I understand what the error means. I do not understand why it is coming up as an error. Here’s the snippet of code …

Total answers: 5

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

super() raises "TypeError: must be type, not classobj" for new-style class Question: The following use of super() raises a TypeError: why? >>> from HTMLParser import HTMLParser >>> class TextParser(HTMLParser): … def __init__(self): … super(TextParser, self).__init__() … self.all_data = [] … >>> TextParser() (…) TypeError: must be type, not classobj There is a similar question on …

Total answers: 7

Preventing a class from direct instantiation in Python

Preventing a class from direct instantiation in Python Question: I have a super class with a method that calls other methods that are only defined in its sub classes. That’s why, when I create an instance of my super class and call its method, it cannot find the method and raises an error. Here is …

Total answers: 4

Why aren't superclass __init__ methods automatically invoked?

Why aren't superclass __init__ methods automatically invoked? Question: Why did the Python designers decide that subclasses’ __init__() methods don’t automatically call the __init__() methods of their superclasses, as in some other languages? Is the Pythonic and recommended idiom really like the following? class Superclass(object): def __init__(self): print ‘Do something’ class Subclass(Superclass): def __init__(self): super(Subclass, self).__init__() …

Total answers: 11