Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
14
votes
5 answers
How would I implement a dict with Abstract Base Classes in Python?
I attempted to implement a mapping in Python by using the abstract base class, MutableMapping, but I got an error on instantiation. How would I go about making a working version of this dictionary that would emulate the builtin dict class, in as…

Russia Must Remove Putin
- 374,368
- 89
- 403
- 331
13
votes
1 answer
python ABC & Multiple Inheritance
I would like to know if it is possible to use multiple inheritance with abstract base class in python. It seems like it should be possible but can't find a statement one way or the other.
The basic ABC example:
from abc import ABC,…

kerzane
- 375
- 3
- 8
12
votes
2 answers
Inheriting from both ABC and django.db.models.Model raises metaclass exception
I am trying to implement a Django data model class, which is also an interface class, using Python 3. My reason for doing so is, I'm writing a base class for my colleague, and need him to implement three methods in all of the classes he derives…

MikeyE
- 1,756
- 1
- 18
- 37
12
votes
1 answer
Python collections ValuesView abc: why doesn't it inherit from Iterable?
I was just checking out some docs on collections.abcs for a project of mine, where I need to do some type-related work. Those are the official docs about the ValuesView type, in both Python 2 and 3:
and this is the source (Python 2, but same happens…

Alan Franzoni
- 3,041
- 1
- 23
- 35
12
votes
2 answers
Why is bytearray not a Sequence in Python 2?
I'm seeing a weird discrepancy in behavior between Python 2 and 3.
In Python 3 things seem to work fine:
Python 3.5.0rc2 (v3.5.0rc2:cc15d736d860, Aug 25 2015, 04:45:41) [MSC v.1900 32 b
it (Intel)] on win32
>>> from collections import Sequence
>>>…

Jaime
- 65,696
- 17
- 124
- 159
12
votes
1 answer
Why is the Callable ABC in the collections ABC module?
The Python collections.abc module contains many handy ABCs for checking various features of objects, but one that doesn't appear to belong is Callable. No standard collection is callable, and PEP 3119 doesn't provide any reasoning or even mention…

Octavia Togami
- 4,186
- 4
- 31
- 49
11
votes
2 answers
Abstract dataclass without abstract methods in Python: prohibit instantiation
Even if a class is inherited from ABC, it can still be instantiated unless it contains abstract methods.
Having the code below, what is the best way to prevent an Identifier object from being created: Identifier(['get', 'Name'])?
from abc import…

Hlib Babii
- 599
- 1
- 7
- 24
11
votes
2 answers
Fetching python class name while using abstract classes with `abc` library
I want to extract the python class name while using abstract classes with abc library. I unfortunately instead receive the class name ABCMeta.
import abc
class A(abc.ABC)
pass
class B(A)
pass
print(A.__class__.__name__) # output:…

Praveen Kulkarni
- 2,816
- 1
- 23
- 39
11
votes
2 answers
Using ABC, PolymorphicModel, django-models gives metaclass conflict
So far every other answer on SO answers in the exact same way: construct your metaclasses and then inherit the 'joined' version of those metaclasses, i.e.
class M_A(type): pass
class M_B(type): pass
class A(metaclass=M_A): pass
class…

mastachimp
- 438
- 3
- 14
11
votes
2 answers
is there a pythonics way to distinguish Sequences objects like "tuple and list" from Sequence objects like "bytes and str"
I have a function like this one
def print_stuff(items):
if isinstance(items, (str, bytes)):
items = (items,)
for item in items:
print (item)
that can be called as follows:
In [37]: print_stuff(('a', 'b'))
a
b
In [38]:…

jcr
- 1,015
- 6
- 18
11
votes
1 answer
Can python abstract base classes inherit from C extensions?
It seems as if that when I have an abstract base class that inherits from gevent.Greenlet (which inherits from the C extension module greenlet: https://github.com/python-greenlet/greenlet) then classes that implement it do not raise any of the abc…

Dustin Wyatt
- 4,046
- 5
- 31
- 60
10
votes
3 answers
Distinguishing between Pydantic Models with same fields
I'm using Pydantic to define hierarchical data in which there are models with identical attributes.
However, when I save and load these models, Pydantic can no longer distinguish which model was used and picks the first one in the field type…

twhughes
- 456
- 6
- 17
10
votes
1 answer
Python type hinting with abstract base classes
I have an ABC with a method that subclasses should return with their own type, and I'm trying to figure out the best way to typehint this. For example:
from abc import ABC, abstractmethod
class Base(ABC):
@abstractmethod
def f(self): ##…

mrip
- 14,913
- 4
- 40
- 58
10
votes
1 answer
Python abc inheritance with specified metaclass
What is the proper way of inheriting from ABC if class have metaclass specified?
Straightforward attempt
class KindMeta(type):
...
class Kind(ABC, metaclass=KindMeta):
...
resulted in TypeError: metaclass conflict: the metaclass of a…

Sadderdaze
- 304
- 3
- 14
10
votes
1 answer
What's the difference between the mro method and the __mro__ attribute of a class?
I stumbled across this extra, no-underscores mro method when I was using __metaclass__ = abc.ABCMeta. It seems to be the same as __mro__ except that it returns a list instead of a tuple. Here's a random example (ideone snippet):
import abc
import…

Air
- 8,274
- 2
- 53
- 88