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 this method within it:
def to_cash_flows(self, notional : float = 100.0) -> fi.asset.CashFlows:
series = pandas.Series(list_of_data, indices_of_data)
return fi.CashFlows(series)
I get the error TypeError: Can't instantiate abstract class CashFlows with abstract methods get_price
when I call to_cash_flows
. I've seen this answer, but am unable to relate it with my current issue.
Thank You
Abstract base classes cannot be instantiated. Instead, they are inherited and extended by the concrete subclasses. Subclasses derived from a specific abstract base class must implement the methods and properties provided in that abstract base class. Otherwise, an error is raised during the object instantiation.
We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.
Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .
To define an abstract method we use the @abstractmethod decorator of the abc module. It tells Python that the declared method is abstract and should be overridden in the child classes. We just need to put this decorator over any function we want to make abstract and the abc module takes care of the rest.
Abstract Classes in Python. Last Updated: 01-04-2020. An abstract class can be considered as a blueprint for other classes. It allows you to create a set of methods that must be created within any child classes built from the abstract class. A class which contains one or more abstract methods is called an abstract class.
This demonstrates that to instantiate a class that derives from an abstract class we have to provide an implementation for all the abstract methods inherited from the parent abstract class. A child class of an abstract class can be instantiated only if it overrides all the abstract methods in the parent class.
A child class of an abstract class can be instantiated only if it overrides all the abstract methods in the parent class. The term override in Python inheritance indicates that a child class implements a method with the same name as a method implemented in its parent class.
An abstract method in Python doesn’t necessarily have to be completely empty. It can contain some implementation that can be reused by child classes by calling the abstract method with super (). This doesn’t exclude the fact that child classes still have to implement the abstract method. Here is an example…
Your CashFlows
class needs to define an implementation of get_price
; it's an abstract method and concrete subclasses must implement it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With