Questions tagged [abc]

Abstract Base Classes are non-instantiable classes used to define the expected behaviour of subclasses.

280 questions
306
votes
6 answers

Why use Abstract Base Classes in Python?

Because I am used to the old ways of duck typing in Python, I fail to understand the need for ABC (abstract base classes). The help is good on how to use them. I tried to read the rationale in the PEP, but it went over my head. If I was looking for…
Muhammad Alkarouri
  • 23,884
  • 19
  • 66
  • 101
100
votes
2 answers

python @abstractmethod decorator

I have read python docs about abstract base classes: From here: abc.abstractmethod(function) A decorator indicating abstract methods. Using this decorator requires that the class’s metaclass is ABCMeta or is derived from it. A class that has a…
Sebastian
  • 4,770
  • 4
  • 42
  • 43
66
votes
6 answers

Python - Testing an abstract base class

I am looking for ways / best practices on testing methods defined in an abstract base class. One thing I can think of directly is performing the test on all concrete subclasses of the base class, but that seems excessive at some times. Consider this…
bow
  • 2,503
  • 4
  • 22
  • 26
60
votes
4 answers

Using abc.ABCMeta in a way it is compatible both with Python 2.7 and Python 3.5

I'd like to create a class which has abc.ABCMeta as a metaclass and is compatible both with Python 2.7 and Python 3.5. Until now, I only succeeded doing this either on 2.7 or on 3.5 - but never on both versions simultaneously. Could someone give me…
Tatiana Al-Chueyr
  • 1,065
  • 1
  • 9
  • 11
51
votes
3 answers

Excluding abstractproperties from coverage reports

I have an abstract base class along the lines of: class MyAbstractClass(object): __metaclass__ = ABCMeta @abstractproperty def myproperty(self): pass But when I run nosetests (which coverage) on my project, it complains that the…
Demian Brecht
  • 21,135
  • 5
  • 42
  • 46
47
votes
3 answers

Determine if a Python class is an Abstract Base Class or Concrete

My Python application contains many abstract classes and implementations. For example: import abc import datetime class MessageDisplay(object): __metaclass__ = abc.ABCMeta @abc.abstractproperty def display(self, message): …
Jeremy
  • 1
  • 85
  • 340
  • 366
43
votes
6 answers

How do I check if a numpy dtype is integral?

How do I check if a numpy dtype is integral? I tried: issubclass(np.int64, numbers.Integral) but it gives False. Update: it now gives True.
Neil G
  • 32,138
  • 39
  • 156
  • 257
39
votes
2 answers

Abstract classes with varying amounts of parameters

I was wondering if its possible when creating an abstract class with abstract methods if its possible to allow the implementations of those methods in the derived classes to have different amounts of parameters for each function. I currently have…
1seanr
  • 657
  • 2
  • 7
  • 18
38
votes
6 answers

Decorators on abstract methods

In python, is there a way to make a decorator on an abstract method carry through to the derived implementation(s)? For example, in import abc class Foo(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod @some_decorator def…
Ian Hincks
  • 3,608
  • 3
  • 23
  • 20
31
votes
2 answers

How to make an Abstract Class inherit from another Abstract Class in Python?

Is it possible to have an Abstract Class inheriting from another Abstract Class in Python? If so, how should I do this?
Squilliam
  • 313
  • 1
  • 3
  • 5
24
votes
1 answer

What is the difference between abstractclass(metaclass=ABCMeta) and class abstractclass(ABC) in Python ABC module?

I have seen two ways for defining Abstract Classes in Python. This one: from abc import ABCMeta, abstractmethod class AbstactClass(metaclass = ABCMeta): And this one: from abc import ABC, abstractmethod class AbstractClass2(ABC): What are there…
user785099
  • 5,323
  • 10
  • 44
  • 62
20
votes
2 answers

Workaround for lack of intersection types with Python generics?

I'm hitting an issue that would be easily solved by intersection types (currently under discussion but not yet implemented) and was wondering what the cleanest workaround is. Current setup and problem My current setup roughly corresponds to the…
Uri Granta
  • 1,814
  • 14
  • 25
17
votes
2 answers

Type-Hinting Child class returning self

Is there any way to type an abstract parent class method such that the child class method is known to return itself, instead of the abstract parent. class Parent(ABC): @abstractmethod def method(self) -> [what to hint here]: …
dbokers
  • 840
  • 1
  • 10
  • 12
14
votes
1 answer

A django model that subclasses an abc, gives a metaclass conflict

I have a following model and abstract base class import abc from django.db import models class AbstractBase(): __metaclass__ = abc.ABCMeta @abc.abstractmethod def my_method(self): return class MyModel(models.Model,…
yilmazhuseyin
  • 6,442
  • 4
  • 34
  • 38
14
votes
2 answers

When should one inherit from ABC?

I always thought one should inherit from abc.ABC when one does not want the class to be instantiated. But I've just realized that if a class has an @abstractmethod then one can also not instanciate it. Is there any other reason to inherit from ABC?
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
1
2 3
18 19