inheritance

Abstract class for thread-related class without multiple inheritance

Abstract class for thread-related class without multiple inheritance Question: I want to create an abstract class for thread-related class. Class that want to enable threading must inherit threading.Thread class, however at the same time, class that want to enable the @abstractmethod must inherit abc.ABC class. Because multiple inheritance is a bad practice, can i achieve …

Total answers: 2

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

Decorate behaviour of subclass methods of an abstract base class

Decorate behaviour of subclass methods of an abstract base class Question: I have an inheritance scenario that looks like this: import abc class A(metaclass=abc.ABCMeta): def __init__(self, x: int | None = None) -> None: self.x = x @abc.abstractmethod def foo(self, x: int | None = None) -> None: pass class B(A): def foo(self, x: int …

Total answers: 1

How can a child class inherit a class method from its parent that gets a class variable from the child in python?

How can a child class inherit a class method from its parent that gets a class variable from the child in python? Question: I have a code here where I define a parent class with class methods and class variables: class Parent: var1 = ‘foo’ var2 = ‘bar’ def getVar1Var2AsString(self): return f'{Parent.getVar1()}_{Parent.getVar2()}’ @classmethod def getVar1(cls): …

Total answers: 3

How to initialize subclasses of an abstract class?

How to initialize subclasses of an abstract class? Question: I’m trying to create a dark souls style text-based game. I’ve created an abstract class which outlines the characteristics of a dark souls class/character type. There are a couple of ways I have initialized it and I’m not sure if one is better than the other. …

Total answers: 1

In Python how to call Parent class function as if I were Parent object

In Python how to call Parent class function as if I were Parent object Question: I have a Parent and a Child class, both should execute their own fct in init but Child have to execute first the Parent fct : class Parent(object): def __init__(self): self.fct() def fct(self): # do some basic stuff class Child(Parent): …

Total answers: 2

Use a class variable across inheritance in Python

Use a class variable across inheritance in Python Question: In Python, I want to define a top level class that can depend on a class variable. Then I want to be able to change that variable at the class level, for children of the class, but still inherit the functionality that uses that variable. In …

Total answers: 2

Setup automatic inheritance of parent class' subclass in Python

Setup automatic inheritance of parent class' subclass in Python Question: When running this code, the print displays the message "no number found in class A", although in fact it was not found in an object of class B. The aim is to change only the Base class in such a way that, when inheriting from …

Total answers: 4

How to intercept the instantiated class name from the function?

How to intercept the instantiated class name from the function? Question: I encountered an issue in one of my projects, I managed to reduce it to a simplest example possible. Consider the following class A: def f(self): return ‘I am f()’ class B(A): def g(self): return ‘I am g()’ a = A() b = B() …

Total answers: 2