Answers for "python: @classmethod"

2

python: @classmethod

class Float:
    def __init__(self, amount):
        self.amount = amount

    def __repr__(self):
        return f'<Float {self.amount:.3f}>'

    @classmethod
    def from_sum(cls, value_1, value_2):
        return cls(value_1 + value_2)


class Dollar(Float):
    def __init__(self, amount):
        super().__init__(amount)
        self.symbol = '€'

    def __repr__(self):
        return f'<Euro {self.symbol}{self.amount:.2f}>'


print(Dollar.from_sum(1.34653, 2.49573))
Posted by: Guest on August-10-2020

Python Answers by Framework

Browse Popular Code Answers by Language