Polymorphism In Python

In Python, how can I practise polymorphism?

Assume we have a very basic version of the option pricing algorithm that requires just three inputs (option type, spot price, and strike price) to compute the intrinsic value of our option. Please see the example code below.

from abc import ABC, abstractmethod
import attr


@attr.s(auto_attribs=True, frozen=True)
class Option(ABC):
    strike_price:float

    @abstractmethod
    def price(self, spot_price: float):
        raise NotImplementedError("This method must be implemented in the subclasses.")

class Call(Option):
    def price(self,spot_price: float) -> float:
        return spot_price - self.strike_price

class Put(Option):
    def price(self,spot_price: float) -> float:
        return self.strike_price - spot_price


def calculate_option_price(option_type:str, spot_price:float, strike_price:float) -> float:
     if option_type == "CALL":
         return Call(strike_price=strike_price).price(spot_price=spot_price)
     elif option_type == "PUT":
         return Put(strike_price=strike_price).price(spot_price=spot_price)

option_price = calculate_option_price(option_type="CALL", spot_price=120, strike_price=80)

print(f"option price is {option_price}.")

Despite the fact that I utilised inheritance to make my code more comprehensible, the implementation of the function "calculate option price" is unstable. This is because a new elif should be written for each new option type in the future. In other words, according to this document, by adding additional elif for each new option type in the future, the implementation size may expand unnecessarily and become more prone to error throughout future updates. How can I overcome that problem in Python by mimicking the behaviour of function pointers in C++ and C#?

Thank you 

  Discussion posts and replies are publicly visible