Questions tagged [abc]

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

280 questions
5
votes
1 answer

Inheriting setter, overwriting getter in python abstract class

Say you have an attribute in a base class with a single setter method that will be used in all subclasses, but with different getter methods in all subclasses. Ideally you only want to write the code for the setter method once in the base class. …
Seanslice
  • 153
  • 6
5
votes
1 answer

abc.abstractmethod + property

According to the docs it should work to combine @property and @abc.abstractmethod so the following should work in python3.3: import abc class FooBase(metaclass=abc.ABCMeta): @property @abc.abstractmethod def greet(self): """…
mortbauer
  • 325
  • 5
  • 11
4
votes
0 answers

All bases of a protocol must be protocols- is typing.MutableMapping not a Protocol?

I want to define a typing protocol for a custom mapping class. This class needs to be very similar to a MutableMapping, except that it has a couple of additional methods beyond those those that collections.abc.MutableMapping defines as abstract…
ThomasNicholas
  • 1,273
  • 11
  • 21
4
votes
0 answers

Python3 abstractmethod, parameters differ from overridden method

The class Parent has an abstract method and I need to use a different number and types of arguments in the method in the children classes/methods. from abc import ABC, abstractmethod class Parent(ABC): """Parent""" @abstractmethod def…
pyjedy
  • 389
  • 2
  • 9
4
votes
1 answer

Python 3.7.4: inheriting both ABC and concrete class

I am modelling a game where each move is the throw of a bomb. There is 1 kind of regular bomb and 3 kinds of special bombs. So, I would like to create a class SpecialBomb that serves as an abstract base for those 3 special bomb classes. The idea is…
4
votes
0 answers

ABC or zope.interface?

I have this code: class AppInterface(zope.interface.Interface): def required_function(): pass @zope.interface.implementer(AppInterface) class DesktopApp: def other_function(self): print('Who I am') I didn't write the…
Hahan't
  • 481
  • 1
  • 4
  • 11
4
votes
2 answers

Why I cant import ABC but ABCMeta is correctly imported?

I got an example code that uses the abc package for python. I installed abc in my laptop using pip. The route to the package folder is correctly set the in PATH. The example code that I got does: 'from abc import ABC, abstractmethod' If I try to…
Anon
  • 146
  • 1
  • 11
4
votes
2 answers

enforcement for abstract properties in python3

I have this abstract class class Kuku(ABC): def __init__(self): self.a = 4 @property @abstractmethod def kaka(self): pass kaka is an abstract property, So I would expect python to enforce it being a property…
omer mazig
  • 965
  • 2
  • 10
  • 17
4
votes
3 answers

Automatic delegation for classes extending python abc classes

If some class extends abc class (Abstract Base Class) then I can't instantiate it unless I define all abstract methods. But often when implementing Decorator pattern, I want to define only a few abstract methods, and others - just delegate to…
yashaka
  • 826
  • 1
  • 9
  • 20
4
votes
2 answers

How do I subclass collections.Iterator?

According to the documentation on ABCs, I should just have to add a next method to be able to subclass collections.Iterator. So, I'm using the following class: class DummyClass(collections.Iterator): def next(self): return 1 However, I…
Jason Baker
  • 192,085
  • 135
  • 376
  • 510
4
votes
3 answers

Static method-only class and subclasses in Python - is there a better design pattern?

I am pricing financial instruments, and each financial instrument object requires a day counter as a property. There are 4 kinds of day counters which have different implementations for each of their two methods, year_fraction and day_count. This…
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
4
votes
1 answer

Implementing Singleton as metaclass, but for abstract classes

I have an abstract class and I would like to implement Singleton pattern for all classes that inherit from my abstract class. I know that my code won't work because there will be metaclass attribute conflict. Any ideas how to solve this? from abc…
Zwierzak
  • 666
  • 1
  • 11
  • 31
4
votes
1 answer

No error while instantiating abstract class, even though abstract method is not implemented

I was trying out the below python code: from abc import ABCMeta, abstractmethod class Bar: __metaclass__ = ABCMeta @abstractmethod def foo(self): pass class Bar2(Bar): def foo2(self): print("Foo2") b = Bar() b2…
codingsplash
  • 4,785
  • 12
  • 51
  • 90
4
votes
1 answer

Python different behaviour with abstractmethod

I have two classes inheriting from the same parent P: from abc import ABCMeta, abstractmethod class P(object): __metaclass__ = ABCMeta @abstractmethod def foo(self): pass class C(P): pass class D(tuple, P): …
wong2
  • 34,358
  • 48
  • 134
  • 179
4
votes
1 answer

Idiomatic multiple inheritance with python Abstract Base Classes

In simplest terms what I want is a tuple with one or two additional methods. __new__ or __init__ are not going to be modified. I would like to create an abstract base class that is subclass of collections.abc.Sequence. Then I want to use it for what…
Krastanov
  • 6,479
  • 3
  • 29
  • 42