inheritance

Python: replacing all appearances of a class constructor in superclass from subclass

Python: replacing all appearances of a class constructor in superclass from subclass Question: This is probably a stupid question. But I encountered this need when writing a tool that needs to slightly modify the API from an existing package. The minimal example is as such class Foo: def __init__(self): print("foo") class Qux(Foo): def __init__(self): print("qux") …

Total answers: 1

How python function A executed when call function B?

How python function A executed when call function B? Question: The existing code is: class Base: def hello(self): print(‘hello’) class A(Base): def greet(self): self.hello() print(‘how are you?’) class B(Base): def greet(self): self.hello() print(‘how are you doing?’) How can I write code to implement that call self.hello() firstly when call self.greet(), but not add self.hello() in …

Total answers: 2

Python3 inheritance from collections.namedtuple: TypeError: object.__init__() takes exactly one argument (the instance to initialize)

Python3 inheritance from collections.namedtuple: TypeError: object.__init__() takes exactly one argument (the instance to initialize) Question: I want to create a child class of collections.namedtuple This is my code: from collections import namedtuple TransactionTuple = namedtuple( typename="Transactionttt", field_names=[ "transactionDate", "valueDate", "value", "currency", "concept" ], defaults=("",) ) class TransactionClass(TransactionTuple): def __init__(self, transactionDate, valueDate, value, currency, concept): super().__init__(transactionDate, …

Total answers: 1

Attribute error: can't set attribute for pygame Sprite inheritance

Attribute error: can't set attribute for pygame Sprite inheritance Question: I have a working code with an HPBar class –> inherits from ProgressBar class –> inherits from pygame.sprite.Sprite. I decided to create a Widget class to have the following inheritance flow: HPBar –> ProgressBar –> Widget –> pygame.sprite.Sprite. The point in doing so is for …

Total answers: 1

When the method __post_init__ doesnt called?

When the method __post_init__ doesnt called? Question: I found the issue where was conversation about an explict call of parent’s __post_init__ method using super(), but if I try this: from dataclasses import dataclass @dataclass class A: def __post_init__(self): print("A") @dataclass class B(A): pass b = B() it will output: A So, parents method works without …

Total answers: 2

Inherit class Decimal, and add another input argument in the constructor

Inherit class Decimal, and add another input argument in the constructor Question: A minimal example to reproduce my problem: from decimal import Decimal class MyDecimal(Decimal): def __init__(self, value, dummy): super().__init__(value) print(dummy) x = MyDecimal(5, ‘test’) Throws: TypeError: optional argument must be a context A similar issue is described in this question, but the answer suggests …

Total answers: 1

Aren’t class attributes inherited?

Aren’t class attributes inherited? Question: I have the following python classes: parent.py #! /usr/bin/env python3 class parent(): CONSTANT = 10 child.py #! /usr/bin/env python3 from parent import parent class child(parent): print(CONSTANT) def my_method(self): print(CONSTANT) But child cannot access CONSTANT. Why doesn’t that work? Is there a way to access the parent’s class attributes (without transforming …

Total answers: 3

How would you decorate without modifying an inherited method?

How would you decorate without modifying an inherited method? Question: I’ve seen and tried the answer given at How would one decorate an inherited method in the child class? to no avail. Sample data: import pandas as pd df = pd.DataFrame([(‘Tom’, ‘M’), (‘Sarah’, ‘X’)], columns=[‘PersonName’, ‘PersonSex’]) I am using the pandera library for DataFrame data …

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