multiple-inheritance

understanding MRO, constructors, and methods in multiple inheritance of python

understanding MRO, constructors, and methods in multiple inheritance of python Question: following is the two set of codes: one having constructor __init__(), and another having display() method. code one: class A(object): def __init__(self): self.a = "a" print(self.a) super().__init__() class B(object): def __init__(self): self.b = "b" print(self.b) super().__init__() class C(A,B): def __init__(self): self.c = "c" print(self.c) …

Total answers: 1

Passing **kwargs arguments to parent classes in multiple inheritance loses kwargs content

Passing **kwargs arguments to parent classes in multiple inheritance loses kwargs content Question: In a multiple inheritance scenario, how does Python3 pass the arguments to all the parent classes? Consider the toy program below: class A(object): def __init__(self, a=’x’, **kwargs): print(‘A’, kwargs) super().__init__() class B(object): def __init__(self, **kwargs): print(‘B’, kwargs) super().__init__() class C(A,B): def __init__(self, …

Total answers: 1

Inheriting more than 1 parent class in python

Inheriting more than 1 parent class in python Question: I am trying to inherit 2 classes as parent class in a child class both have different class variables class A has a,b while class B has c,d,e they are the parent to class AB(A,B) now i want to make an object of AB class but …

Total answers: 2

How to make a non-overriding method stub in Python multi-inheritance?

How to make a non-overriding method stub in Python multi-inheritance? Question: Imagine that you have 2 mixin classes, that each define abstract methods and implementations. Together they implement every method, but depending on the inheritance order, the empty stubs will overwrite the implementation from the other class. There’s at least two ways to overcome this …

Total answers: 2

How Python's Super() works in multiple inheritance for superclasses?

How Python's Super() works in multiple inheritance for superclasses? Question: I’m trying to understand how super works in python’s multiple inheritance for the superclasses, for example in the code below: class First(): def __init__(self, parm1, **kwargs): super().__init__(**kwargs) self.parm1 = parm1 self.parm3 = ‘one’ class Second(): def __init__(self, parm2 = ‘zero’, **kwargs): super().__init__(**kwargs) self.parm2 = parm2 …

Total answers: 2

Python Multiple Inheritance super().__init__()

Python Multiple Inheritance super().__init__() Question: I have two classes, with the same parameter initialized by their __init__ method. I would like to inherit both classes in class "X". But I will get: TypeError: B.__init__() missing 1 required positional argument: ‘my_param’ Reproducible Example: class A: def __init__(self, my_param): super().__init__() self.my_param = my_param class B: def __init__(self, …

Total answers: 2

Python Airflow custom sensor – which methods to implement

Python Airflow custom sensor – which methods to implement Question: I’m trying to write a custom sensor that inherits BaseSensorOperator and SSHOperator. in the sensor body I override poke and execute method. class mysensor (BaseSensorOperator,SSHOperator): template_fields = ("my_param") def __init__ ( self, my_param = None, *args, **kawags): self.my_param = my_param super().__init__( command = "some bash …

Total answers: 1

Multiple inheritance using typing module and mypy

Multiple inheritance using typing module and mypy Question: Consider the following code: from typing import Union class A: def function_in_a(self) -> str: return ‘a’ class B: def function_in_b(self) -> str: return "b" class C(A, B): pass def call_functions(c: Union[A, B]) -> None: print(c.function_in_a()) print(c.function_in_b()) if __name__=="__main__": c = C() call_functions(c) Note that the function call_functions …

Total answers: 2

Python mixin with abstract methods overrides concrete methods

Python mixin with abstract methods overrides concrete methods Question: Two mixin classes specify requirements as abstract methods. Together, the classes have a full set of concrete methods. However, they fail to combine into a concrete class: no matter which order I use to declare the concrete class, some abstract methods override the concrete ones. Is …

Total answers: 1

python multiple inheritance passing arguments to constructors using super

python multiple inheritance passing arguments to constructors using super Question: Consider the following snippet of python code class A(object): def __init__(self, a): self.a = a class B(A): def __init__(self, a, b): super(B, self).__init__(a) self.b = b class C(A): def __init__(self, a, c): super(C, self).__init__(a) self.c = c class D(B, C): def __init__(self, a, b, c, …

Total answers: 4