abstract-class

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

How do I correctly code an abstract class and its subclasses to have getter/setter behavior?

How do I correctly code an abstract class and its subclasses to have getter/setter behavior? Question: In Python (3.11) I have this abstract class: from abc import ABCMeta, abstractmethod from copy import deepcopy class BookPrototype(metaclass=ABCMeta): @property def title(self): pass @title.setter @abstractmethod def title(self, val): pass @abstractmethod def clone(self): pass I create this subclass: class ScienceFiction(BookPrototype): …

Total answers: 1

type hint for generic attribute inside abstract class

type hint for generic attribute inside abstract class Question: I have a convoluted problem with types that I canĀ“t solve. Let’s say I have some abstract classes and a generic type equivalent to this from abc import ABC from typing import Generic, TypeVar class Action(ABC): pass ActionTypeVar = TypeVar("ActionTypeVar", bound="Action") class Agent(ABC, Generic[ActionTypeVar]): actions: tuple[ActionTypeVar, …

Total answers: 1

Specify return type of a wrapper function that calls an abstract method in Python

Specify return type of a wrapper function that calls an abstract method in Python Question: For this example, consider the simplified scenario where a Solver will return a Solution. We have Solutions: class Solution(ABC): pass class AnalyticalSolution(Solution): pass class NumericalSolution(Solution): def get_mesh_size(self) -> float: return 0.12345 And Solvers: class Solver(ABC): def solve(self, task: int) -> …

Total answers: 1

How to declare instance variables in abstract class?

How to declare instance variables in abstract class? Question: class ILinkedListElem: @property def value(self): raise NotImplementedError @property def next(self): raise NotImplementedError class ListElem(ILinkedListElem): def __init__(self, value, next_node=None): self.value = value self.next = next_node I wanna something like this. This abstract variables definition works for class vars, but not for instance I want to all instances …

Total answers: 1

How to create property methods in django models dynamically?

How to create property methods in django models dynamically? Question: I am creating property methods for every model where the model attribute includes ImageField or FileField. So, I decided to make an abstract model where I check the fields in the model and if there are any ImageField and FileField in the model the property …

Total answers: 1

Best way to create classes that only differ by one method?

Best way to create classes that only differ by one method? Question: I am implementing multiple RL agents which share a lot of common attributes and methods but differ in only one. Namely the one that calculates the td_error. Out of the top of my head I can think of 3 options to implement this: …

Total answers: 1

Why can I instantiate an abstract class without implementing an abstractmethod?

Why can I instantiate an abstract class without implementing an abstractmethod? Question: Trying to understand abstract classes, I created a simple model: from abc import ABC, abstractmethod class Publication(ABC): def __init__(self, title): self.title = title @abstractmethod def Description(self): pass class Periodical(Publication): def __init__(self, title, publisher): super().__init__(title) self.publisher = publisher class Book(Publication): def __init__(self, title, author): …

Total answers: 1