Home > database >  2 constructor class in 1 constructor class in Python
2 constructor class in 1 constructor class in Python

Time:07-28

I'm trying to put 2 constructor class in 1 constructor class with this code but:

class Buy:
    def __init__(self, buy_value: float, buy_currency: str) -> None:
        self.buy_value = buy_value
        self.buy_currency = buy_currency
class Sell:
    def __init__(self, sell_value: float, sell_currency: str) -> None:
        self.sell_value = sell_value
        self.sell_currency = sell_currency

class isCash(Buy,Sell):
    def __init__(self, buy_value, buy_currency,sell_value, sell_currency) -> None:
        super().__init__(buy_value, buy_currency)
        super().__init__(sell_value,sell_currency)


obj1 = isCash(Buy(1,"mnt"),Sell(2,"mnt"))
print(obj1.__dict__)

Result

{'buy_value': 2, 'buy_currency': 'mnt'}

as you can see sell DICT is gone. Is there anyway that i can achieve this.

{
 "buy": {
   "buy_value": 1,
   "buy_currency": "MNT"
 },
 "sell": {
   "sell_value": 1,
   "sell_currency": "MNT"
 },
}

CodePudding user response:

Your both super() calls constructors up to Buy, so Sell always gets ignored. You need to modify the second super to explicitly call constructors after Buy. And you can update your Buy and Sell class to achieve your expected dict. You should also correct your initialization.

class Buy:
    def __init__(self, buy_value: float, buy_currency: str) -> None:
        self.buy = {
            'buy_value': buy_value,
            'buy_currency': buy_currency.upper()
        }

class Sell:
    def __init__(self, sell_value: float, sell_currency: str) -> None:
        self.sell = {
            'sell_value': sell_value,
            'sell_currency': sell_currency.upper()
        }

class isCash(Buy,Sell):
    def __init__(self, buy_value, buy_currency,sell_value, sell_currency) -> None:
        super().__init__(buy_value, buy_currency)
        super(Buy, self).__init__(sell_value, sell_currency)

obj1 = isCash(1,"mnt",2,"mnt")
print(obj1.__dict__)

Output:

{'buy': {'buy_value': 1, 'buy_currency': 'MNT'},
 'sell': {'sell_value': 2, 'sell_currency': 'MNT'}}

CodePudding user response:

super().__init__ calls ALL of the parent classes, in a defined order. Thus, your second one overwrites the results of the first. You need to use explicit forwarding:

    Buy.__init__(self, buy_value, buy_currency)
    Sell.__init__(self, sell_value, sell_currency)

However, the way you're calling isCash is wrong. What you have would require:

obj1 = isCash(1,"mnt",2,"mnt")

Did you actually want to use encapsulation, instead of inheritance?

class isCash:
    def __init__(self, buy, sell):
        self.buy = buy
        self.sell = sell

That definition matches your usage.

CodePudding user response:

If you intended to the dictionary as two different attributes of the class then you do not need inheritence which will call the constructors in the method resolution order(See here) and instead need encapsulation. The code as follows should work fine with this implementation:

import json

class Buy:
    def __init__(self, buy_value: float, buy_currency: str) -> None:
        self.buy_value = buy_value
        self.buy_currency = buy_currency
class Sell:
    def __init__(self, sell_value: float, sell_currency: str) -> None:
        self.sell_value = sell_value
        self.sell_currency = sell_currency

class isCash:
    def __init__(self, buy_value, buy_currency,sell_value, sell_currency) -> None:
        self.buy = Buy(buy_value, buy_currency)
        self.sell = Sell(sell_value,sell_currency)


obj1 = isCash(1,"mnt",2,"mnt")
print(json.loads(json.dumps(obj1, default=lambda o: o.__dict__)))
  • Related