Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
4
votes
3 answers
Implementing pointwise arithmetic with implicit type conversion
Suppose I have class Function, whose instances are callables that take one argument. I defined pointwise arithmetic for these classes in the straightforward way. Here's a simplified version of my code (I actually have more complex behavior in…

max
- 49,282
- 56
- 208
- 355
4
votes
4 answers
Python idiom for dict-able classes?
I want to do something like this:
class Dictable:
def dict(self):
raise NotImplementedError
class Foo(Dictable):
def dict(self):
return {'bar1': self.bar1, 'bar2': self.bar2}
Is there a more pythonic way to do this? For…

Andrew Lee
- 2,543
- 3
- 20
- 31
3
votes
1 answer
Pattern matching to check a protocol. Getting TypeError: called match pattern must be a type
I need to match cases where the input is iterable. Here's what I tried:
from typing import Iterable
def detector(x: Iterable | int | float | None) -> bool:
match x:
case Iterable():
print('Iterable')
return…

Raymond Hettinger
- 216,523
- 63
- 388
- 485
3
votes
1 answer
How come an abstract base class in python can be instantiated?
It is very surprising to me that I can instantiate an abstract class in python:
from abc import ABC
class Duck(ABC):
def __init__(self, name):
self.name = name
if __name__=="__main__":
d = Duck("Bob")
print(d.name)
The above…

user32882
- 5,094
- 5
- 43
- 82
3
votes
1 answer
TypeError: __init_subclass__() takes no keyword arguments related to subclass and abstract class design
I implemented the following design using abstract class and its subclass class as follows
from abc import ABC, abstractmethod
class Pipeline(ABC):
@abstractmethod
def read_data(self):
pass
def __init__(self, **kwargs): …

user785099
- 5,323
- 10
- 44
- 62
3
votes
2 answers
Use of ABCMeta.register
What is the use of ABCMeta's register method?
https://docs.python.org/3/library/abc.html
It seems that if I have an abstract base class ABC, say, Animal, I can register a subclass of it using register:
from abc import ABC
class Animal(ABC):
…

Michele Piccolini
- 2,634
- 16
- 29
3
votes
0 answers
Can I have kwargs in abstract base class, but specific parameters in subclasses?
Python 3.7+: I'm trying to define an ABC with an abstract method:
from abc import ABC, abstractmethod
class A(ABC):
@abstractmethod
def f(self, x, **kwargs):
pass
I would like to define, in subclasses, the specific parameters each…

ragazzojp
- 477
- 3
- 14
3
votes
1 answer
How to use self in an abstract class implementation in Python?
I'm working on a project using abstract classes in Python (specifically, the abc module).
I have a few implementations of this abstract class, which have their own constructors and need to use self.
This is what my code looks like, but…

jack.py
- 362
- 8
- 23
3
votes
2 answers
Python determine if class is abstract (ABC) without abstractmethod
I have a class that inherits from ABC, and does not have any abstractmethod.
I would like to check if it's an abstract class, and am currently stumped.
Determine if a Python class is an Abstract Base Class or Concrete prescribes using…

Intrastellar Explorer
- 3,005
- 9
- 52
- 119
3
votes
1 answer
Python Library for Function Interfaces?
I'm wondering if there's an existing Python libary/technique for enforcing function interfaces/"contracts". Something like ABC but for functions.
E.g. An example with made-up syntax:
@implements(i_state_updater)
def my_update(position, state,…

Peter
- 12,274
- 9
- 71
- 86
3
votes
1 answer
Inherited Abstract Classes in python
In python, can I define an interface (abstract class) by inheritance from another abstract class?
If I try:
import abc
ABC = abc.ABCMeta('ABC', (object,), {})
class interface(ABC):
@abc.abstractmethod
def method(self, message):
…

dario
- 145
- 5
3
votes
3 answers
music21 format stream as ABC+ and save as a file
i'm trying to convert the whole Bach corpus (mxl files) to abc files.
is this possible within music21?
thanks!
v

Van Anders
- 33
- 2
3
votes
1 answer
In Python, how to enforce an abstract method to be static on the child class?
This is the setup I want:
A should be an abstract base class with a static & abstract method f(). B should inherit from A. Requirements:
1. You should not be able to instantiate A
2. You should not be able to instantiate B, unless it implements a…

GrowinMan
- 4,891
- 12
- 41
- 58
3
votes
1 answer
class attribute considered abstract method in python 2.7 - abc module
I am trying to implement an abstract superclass (Base) with an abstract method (addfeature), which the Child class will override.
from lxml.builder import ElementMaker
from abc import ABCMeta, abstractmethod
class Base(object):
__metaclass__ =…

Harry G.
- 45
- 5
2
votes
1 answer
collections.abc.Iterable doesn't allows runtime structural checks according to Iterable API
Existing Approaches to Structural Subtyping
Abstract classes defined in collections.abc module are slightly more advanced since they implement a custom __subclasshook__() method that allows runtime structural checks without explicit…

Dorian Turba
- 3,260
- 3
- 23
- 67