Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
1
vote
1 answer
Requiring Child Classes to call base.Foo() in overrides
I'm working on a Unity project which uses a good amount of inheritance. I have an abstract base class whose methods I want its children to always call (e.g. Awake).
I haven't worked with attributes much - is there a way to add an attribute to my…

Derek Rocco
- 11
- 1
1
vote
0 answers
Decorate abstract method
I am trying to decorate an @abstractmethod in an abstract class (inherited by abc.ABC in Python 3.9)
As a MWE,
from abc import ABC, abstractmethod
class Block(ABC):
def __init__(self,id=1):
self.id=id
@abstractmethod # the method…

gtn_brsc
- 11
- 1
1
vote
0 answers
Is there any point in inheriting ABC Class in Python if you do not have any abstract methods inside?
# For simplicity, we are not defining getter and setter functions. The reader can
# assume that all class attributes are private and accessed through their respective
# public getter methods and modified only through their public methods…

Vijay Raja
- 11
- 2
1
vote
0 answers
DRF create custom permission class as an abstract class
I want to create a custom Django rest framework permission class by subclassing BasePermission, but I would also like it to be an abstract class. When doing so I get following error:
from abc import ABC, abstractmethod
from…

Aseem
- 5,848
- 7
- 45
- 69
1
vote
0 answers
Enforce/Define python classes with only the specified attributes
I want to create some kind of structure, where the classes that inherit the ABC class can only have the methods that are defined in it and cannot have any other methods in it.
My abc looks like
from abc import ABCMeta, abstractmethod
class…

random_npc
- 171
- 1
- 12
1
vote
2 answers
Assign CSV values to class attributes, process output to csv using abstract classes
This question is a bit confusing so bear with me, it also has to be done with pure Python and no third-party modules.
I can't seem to assign the correct datatype to the CSV values and assign them to the class attributes.
I've tried every way I know,…

chocLami
- 35
- 5
1
vote
0 answers
How to force subclasses to implement a field using abstract base class in Python?
Abstract base class can force implementation of a method by all it's child classes using @abstractmethod decorator. Can the same be done with class fields?
Say we have the following classes:
from abc import ABCMeta, abstractmethod
class…

True Nerd Of Botva
- 41
- 4
1
vote
2 answers
Why is str not a subclass of collections.abc.ByteString?
When having a look at the types str and bytes in Python, it turns out they are very similiar. The only differences wrt. their attributes are:
>>> set(dir(bytes)) - set(dir(str))
{'hex', 'fromhex', 'decode'}
>>> set(dir(str)) -…

Franz
- 357
- 3
- 11
1
vote
2 answers
Abc import data from local sql server to local elasticsearch
I am trying to use the excellent tool ABC from appbase.io (https://github.com/appbaseio/abc) to import data from my local sql server instance to my local elasticsearch instance (running on a docker container)
When I run the following command to…

Augusto Spinelli
- 21
- 4
1
vote
1 answer
Initializing common attributes in the base class in Python
I have the following case with abstract classes - there is a base class A and, say, inheriting classes B and C. B and C have some attributes initialized in their own way, however, there are some attributes that should have the initial value same for…

Anastasia
- 23
- 3
1
vote
0 answers
Dangers of overriding a dict in Python
I came across this question of trying to implement a dictionary using the collections.abc MutableMapping because I was looking for something similar.
For context, I was looking to implement a dictionary that would out of convenience also act as a…

d34n
- 52
- 1
- 7
1
vote
1 answer
Implement a type in Python, for functions in a module (not class)?
def f(x, y):
return x & 1 == 0 and y > 0
g = lambda x, y: x & 1 == 0 and y > 0
Now the same thing in Haskell:
import Data.Bits
f :: Int -> Int -> Bool
f x y = (.&.) x 1 == 0 && y > 0
That works, however this doesn't:
g = \x y -> (.&.) x 1 ==…

A T
- 13,008
- 21
- 97
- 158
1
vote
0 answers
Python inherit abc.ABC and typing.SupportsBytes at the same time
I am new to writing typed Python code, but I have experience with OOP from other languages. I want to create an abstract class, that also can be serialized using bytes function. I correctly implement __bytes__ method, but I would like to explicitly…

Vojtech Kane
- 559
- 6
- 21
1
vote
1 answer
Implement abstract properties without redefining __init__ in child
I am learning the abc module and was wondering if what I want to do is possible.
Essentially, every child I make of the base class should have the SAME exact __init__. The base class will have a few abstract properties that will need to be defined…

Melendowski
- 404
- 4
- 16
1
vote
1 answer
Create an ABC with abstract methods defined from json keys
Say I have a json file look like:
{
"foo": ["hi", "there"],
"bar": ["nothing"]
}
I'd like to create an abstract base class (ABC), where the name of abstract methods are the keys of the json above, i.e.:
from abc import ABCMeta,…

ytu
- 1,822
- 3
- 19
- 42