Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
0
votes
1 answer
Detect incomplete subclass of abstract class, python
In Python, how can I differentiate between a concrete subclass and a subclass which is still abstract (i.e. not all abstract methods have been implemented)?
Consider the following:
import abc
class A(abc.ABC):
@abc.abstractmethod
def…

RJ-Adam
- 969
- 1
- 6
- 9
0
votes
0 answers
Meaning of def(_f) and del(_f) at the beginning of ABC collections
Just went across internals of ABC collections.
Can anyone please explain what does this construct stand for:
def _f(): pass
FunctionType = type(_f)
del _f
I know I'm probably missing some basic concept(s), please advise where to start/look to…

vtm11
- 165
- 1
- 9
0
votes
0 answers
How can I use python's ABC library for nested abstractmethods to build an interface for Airflow2.X?
I am using Airflow2.0's taskflow API to generate DAGs in order to orchestrate ETL jobs.
Airflow2.0 doesn't seem to provide a framework to generate DAGs according to the DRY principle. Basically each DAG needs to be generated in a separate file and…

omoshiro
- 3
- 3
0
votes
1 answer
class B derived from an abstract base class A, and how can i use singleton in class B?
below is the demo code:
class A {
public:
A(){}
virtual void method()=0;
//....
virtual ~A(){};
}
class B : public A{
static A * ptr;
//....
public:
//....
static A* GetInstance() {
if (ptr == nullptr)
…

FLYFLY
- 49
- 6
0
votes
0 answers
Which way is better to define a python abstract class? Why?
I wrote two code snippets here and I think they both do the job. However, I need to understand why one is better than the other.
This implementation doesn't leverage @property and I don't need it to since I can just use self in the Honda class for…

BeetleJuice
- 31
- 3
0
votes
0 answers
Get error or warning for this incorrect inheritance structure?
I'd like to make my python IDE give me an error or warning when I implement derived classes that break the base class contract and return the wrong type.
( By the way this is not a theoretical interest question, this kind of mistake actually…

Gonen I
- 5,576
- 1
- 29
- 60
0
votes
1 answer
Do I need isinstance checks when using ABCs?
Here is an example of an interface as seen in the "Fluent Python" book:
from abc import ABCMeta, abstractmethod
class IStream(metaclass=ABCMeta):
@abstractmethod
def read(self, maxbytes=-1):
pass
@abstractmethod
def…

barciewicz
- 3,511
- 6
- 32
- 72
0
votes
1 answer
maximum recursion depth exceeded: abstract property inheritance in python
I am trying to define an abstract class with an abstract property that can then be inherited in a concrete class. But it is giving me maximum recursion depth exceeded.
Vechile is an abstract class and Car implements that class. The base class has a…

Koushik Saha
- 673
- 1
- 10
- 25
0
votes
0 answers
Can a metaclass Singleton extend an ABC?
One of the ways to cope with the static nature of Singeltons is to hide them behind interfaces. This allows to use Singletons in some clients, but to replace them with substitutes such as Mocks in tests.
Can I make a Python Metaclass Singleton…

Gonen I
- 5,576
- 1
- 29
- 60
0
votes
0 answers
"method incompatible with supertype" when using *args: Any
I've been working on a class that utilises abstract methods. However, I'm getting a mypy error for overwriting the function declaration in each child class. Could someone please explain to me how I can achieve the same effects, whilst also obeying…

lewiswolf
- 119
- 1
- 6
0
votes
1 answer
How to implement abstract classes over mulitple inheritances?
I have a question on multi level inheritance.
I am trying to write classes of the form:
from abc import ABC, abstractmethod
import numpy as np
### Parent class
class A(ABC):
@abstractmethod
def eval(self, x: np.ndarray) -> np.ndarray:
…

NMme
- 461
- 3
- 12
0
votes
1 answer
How exactly does Python ABC interface work?
I was reading the book Fluent Python by Luciano Ramalho and I encountered the following line:
Note that the built-in concrete sequence types do not actually
subclass the Sequence and MutableSequence abstract base classes (ABCs)
depicted
So I…

Shiladitya Bose
- 893
- 2
- 13
- 31
0
votes
1 answer
How to let the constructor in the subclass inherit the constructor or base class?
I have the following code segment, which is shown as follows along with its output
from abc import ABCMeta, abstractmethod
class AbstractClass(object, metaclass=ABCMeta):
@abstractmethod
def __init__(self, n):
self.n = n
…

user297850
- 7,705
- 17
- 54
- 76
0
votes
1 answer
the usage of adding @property before an @abstractmethod function in Python
In the design of the abstract base class, I can find the following scenarios. What are the design considerations of adding @property for a method in the abstract base class. When I implement the related function, e.g., f1 here, are there any…

user785099
- 5,323
- 10
- 44
- 62
0
votes
0 answers
Python3: Do subclasses of an abstract class inherit its magic methods?
Here's the tea: I'm writing a small Monopoly game using python. I've made this little class family to represent the bills. There's a base abstract class called Bill which inherits from the abc.ABC class.
Code:
from abc import ABC,…

Jamba
- 59
- 1
- 5