Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Can't instantiate abstract class in Python

Tags:

python

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

like image 710
user131983 Avatar asked Aug 12 '15 19:08

user131983


People also ask

Can you instantiate an abstract class Python?

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.

Why can't you instantiate an abstract class?

We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

How do you instantiate an abstract class?

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 .

How do you declare a class abstract in Python?

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.

What is an abstract class in Python?

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.

How to instantiate a class that derives from 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.

How do you instantiate a child class in Python?

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.

Does an abstract method have to be empty in Python?

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…


1 Answers

Your CashFlows class needs to define an implementation of get_price; it's an abstract method and concrete subclasses must implement it.

like image 159
Wooble Avatar answered Sep 22 '22 16:09

Wooble