0
class Option:
    order_type: str
    pair: str

    def __init__(self, pair, order_type) -> None:
        self.order_type = order_type
        self.pair = pair

class Order(Option):

    price: float
    quantity: float
    
    def __init__(self, pair, order_type, price, quantity) -> None:
        Option.__init__(self, pair, order_type)
        self.price = price
        self.quantity = quantity


order = Order("btcusdt", "sell", "1", "1")

I want to get option from order object like.

option = order as Option
order.option
yEmreAk.com
  • 3,278
  • 2
  • 18
  • 37
  • "I want to get `option` from `order` object like." Why? What will you do with this `option` after you get it? – Code-Apprentice Apr 16 '22 at 15:44
  • 1
    Also, why does `Order` derive `Option`? Remember that inheritence describes an "is-a" relationship. Does it make sense to say "an order is an option"? Or would it make more sense to say "an order **has** an option"? If the later, then you should include an `option` field on the `Order` class instead of using inheritence. – Code-Apprentice Apr 16 '22 at 15:46
  • You are right. Maybe I misunderstand inheritance relationship. I'll include option field to Order class, thank you so much :) – yEmreAk.com Apr 16 '22 at 16:33

1 Answers1

0

This is not really like inheritance works in Python.

One option would be to have a class method te recreate the Option object.

class Option:
    order_type: str
    pair: str

    def __init__(self, pair, order_type) -> None:
        self.order_type = order_type
        self.pair = pair

    @classmethod
    def from_order(cls, the_order):
        the_obj = cls(the_order.pair, the_order.order_type)
        return the_obj


class Order(Option):

    price: float
    quantity: float

    def __init__(self, pair, order_type, price, quantity) -> None:
        Option.__init__(self, pair, order_type)
        self.price = price
        self.quantity = quantity

I must admit that I don’t see concrete example where this should be usefull.

Floh
  • 745
  • 3
  • 16