Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.
Questions tagged [abc]
280 questions
0
votes
0 answers
Is abc.ABC available in python 2.7.16?
Based on https://docs.python.org/2/library/abc.html, it seems abstract classes are available starting in version 2.6 (the top says "New in version 2.6.")
However, I cannot
from abc import ABC, abstractmethod using python 2.7.16 as it produces the…

David
- 619
- 2
- 8
- 15
0
votes
0 answers
Automatic attribute copying from member class to parent class at class definition using ABC's __init_subclass__ in Python
I have this code:
from abc import ABC
class ConfigProto(ABC):
def __init__(self, config_dict):
self._config_dict = config_dict
def parse(self, cfg_store: dict) -> None:
# do something with the config dict and the cfg_store…

Adam Bajger
- 523
- 7
- 20
0
votes
2 answers
How to manipulate cluster data point of Kmeans clustering algorithm
In Kmeans clustering we can define number of cluster. But is it possible to define that cluster_1 will contain 20% data, cluster_2 will have 30% and cluster_3 will have rest of the data points?
I try to do it by python but couldn't.
0
votes
0 answers
abc.py __instancecheck__ taking an inordinate amount of time for large calculation. Is there a way around it without abandoning python?
I wrote a program for calculating certain polynomials in python and it's reasonably fast, but I ran cProfile on it and the results are disturbing. The specific run takes 296 seconds, which is fine, but the cumulative time spent in abc.py…

Matt Samuel
- 133
- 10
0
votes
1 answer
How to pprint custom collection
I am not able to simulate pprint on my custom collections.
See the following behaviour:
>>>from pprint import *
>>>pprint(["x"*80]*4)
['xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
…

BorjaEst
- 390
- 2
- 11
0
votes
1 answer
BLIF outputed by yosys involves DFF, and the BLIF file cannot be read by ABC
I'm new to yosys and ABC for logic synthesis. I downloaded one design aes_core from opencores, and run the following script with yosys to map the design into blif:
read_verilog ./designs/apbtoaes128/trunk/rtl/*.v
hierarchy -check -top…

Pu Yuan
- 1
- 1
0
votes
1 answer
How to override Integer in Python?
I want to inherit from integers and only redefine some methods.
The goal is to have this behaviour:
>>> i = Iter()
>>> i == 0
True
>>> next(i)
Iter<1>
>>> next(i)
Iter<2>
>>> i + 10
12
The naive approach would be to inherit from int:
class…

nowox
- 25,978
- 39
- 143
- 293
0
votes
0 answers
Validate subclasses' variables for uniqueness
Suppose I have a simple ABC and some subclasses
from abc import ABC
class BaseClass(ABC):
var: str
class Child1(BaseClass):
var = 'foo'
class Child2(BaseClass):
var = 'foo' # I want exception here or do not count this class as a…

Ivan Trushin
- 198
- 1
- 7
0
votes
2 answers
Python TypeError: 'float' object is not callable - Using @property decorator, abstract classes, and super()
I created a short code to learn a bit more about abstract classes and the getter decorator in python as well as super. Here is my code:
from abc import ABC, abstractmethod
class Abstract_class(ABC):
@abstractmethod
def cal(self):
…

jei L
- 33
- 6
0
votes
0 answers
Allow mutable values in custom dictionary to be mutated on the fly in Python
I am creating custom on-disk dictionary, while trying to be as close to dict as possible, in terms of abilities and interface, while trying to keep interface minimal.
Currently I'm stuck with the problem that dict allows to mutate its elements,…

Timofey Bakunin
- 23
- 1
- 6
0
votes
1 answer
Non-overriden abstract-static method does not throw error
i have come across some weird behavior when using ABC library in Python 3.8.10.
from abc import ABC, abstractproperty, abstractstaticmethod, abstractmethod
from dataclasses import dataclass
@dataclass
class Message(ABC):
_type: str = None
…

Jan Janáček
- 109
- 1
- 9
0
votes
1 answer
Why does the super() call work in a Python class that derives from an ABC?
I have a Python ABC (let's call it A) with a concrete __init__() method, as well as a class that implements this ABC (let's call it B). As far as I understand, I should not be able to instantiate the ABC.
Question: Why and how is the call…

Mowgli
- 157
- 1
- 10
0
votes
1 answer
Properly backporting generic collections
In Python 3.9 it is now possible to use collections.abc with generics, meaning code such as this is possible:
import collections.abc
from typing import TypeVar
T = TypeVar("T")
class MySequence(collections.abc.Sequence[T]):
...
In Python…

Simply Beautiful Art
- 1,284
- 15
- 16
0
votes
0 answers
Is it possible to enforce a return type with an interface using python ABC?
Say I have the following code sample. I want the 'walk' function to return a dictionary for all animals, I am using the interface to enforce this. However, below code sample will run just fine. Is it possible to modify the interface so that it…

Rodi
- 122
- 8
0
votes
1 answer
Pure Python abc/ABCMeta implementation?
It would be interesting to read the original pure Python implementation of abc module, described in PEP 3119.
Unfortunately, the link to a sample implementation from original PEP leads to a password protected SVN repository on python.org. I don't…

lithuak
- 6,028
- 9
- 42
- 54