abstract-class

Force an abstract class attribute to be implemented by concrete class

Force an abstract class attribute to be implemented by concrete class Question: Considering this abstract class and a class implementing it: from abc import ABC class FooBase(ABC): foo: str bar: str baz: int def __init__(self): self.bar = "bar" self.baz = "baz" class Foo(FooBase): foo: str = "hello" The idea here is that a Foo class …

Total answers: 3

How define constructor implementation for an Abstract Class in Python?

How define constructor implementation for an Abstract Class in Python? Question: I am trying to declare an abstract class A with a constructor with a default behavior: all subclasses must initialize a member self.n: from abc import ABCMeta class A(object): __metaclass__ = ABCMeta def __init__(self, n): self.n = n However, I do not want to …

Total answers: 6

How to write to an abstract property in Python 3.4+?

How to write to an abstract property in Python 3.4+ Question: In Python 3.6, Let’s say I have an abstract class MyAbstractClass from abc import ABC, abstractmethod class MyAbstractClass(ABC): @property @abstractmethod def myProperty(self): pass and a class MyInstantiatableClass inherit from it. So how do I write to the property myProperty on instantiation of an object …

Total answers: 2

What should I put in the body of an abstract method?

What should I put in the body of an abstract method? Question: Say I have the following abstract class Foo: import abc class Foo(abc.ABC): @abc.abstractmethod def bar(self): raise NotImplementedError What should I put in the body of the bar method? I see a lot of code that has raise NotImplementedError, as shown above. However, this …

Total answers: 2

python subclasscheck & subclasshook

python subclasscheck & subclasshook Question: The methods __subclasscheck__ and __subclasshook__ are used to determine if a class is regarded as subclass of another. However, their documentation is very limited, even in advanced Python books. How are they meant to be used and what is their difference (higher priority, side of relationship they refer to etc…)? …

Total answers: 2

"TypeError: Can't instantiate abstract class" in Python

"TypeError: Can't instantiate abstract class" in Python Question: I have a module fi with the following classes defined: class Asset(metaclass=abc.ABCMeta): pass @abc.abstractmethod def get_price(self, dt : datetime.date, **kwargs): ”’ Nothing here yet ”’ class CashFlows(Asset): def __init__(self, amounts : pandas.Series, probabilities : pandas.Series = None): amounts = Asset.to_cash_flows() I then have another class Bond(fi.Asset) with …

Total answers: 2

Can't instantiate abstract class … with abstract methods

Can't instantiate abstract class with abstract methods Question: I’m working on a kind of lib, and I’m getting an error. Here is my code. Of course @abc.abstractmethod have to be uncommented Here are my tests Sorry couldn’t just copy and paste it I went on the basis that the code below works. test.py: import abc …

Total answers: 4

Python abstract classes – how to discourage instantiation?

Python abstract classes – how to discourage instantiation? Question: I come from a C# background where the language has some built in “protect the developer” features. I understand that Python takes the “we’re all adults here” approach and puts responsibility on the developer to code thoughtfully and carefully. That said, Python suggests conventions like a …

Total answers: 6

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

Determine if a Python class is an Abstract Base Class or Concrete Question: 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): pass class FriendlyMessageDisplay(MessageDisplay): def greet(self): hour = datetime.datetime.now().timetuple().tm_hour if hour < 7: raise Exception(“Cannot greet while asleep.”) elif …

Total answers: 3

Is it possible to make abstract classes in Python?

Is it possible to make abstract classes? Question: How can I make a class or method abstract in Python? I tried redefining __new__() like so: class F: def __new__(cls): raise Exception("Unable to create an instance of abstract class %s" %cls) But now, if I create a class G that inherits from F like so: class …

Total answers: 14