Код: Выделить всё
class Percent(float):
def __init__(self, x):
super().__init__(x / 100)
print(float(Percent(12)))
# I want 0.12
Код: Выделить всё
Traceback (most recent call last):
File "
", line 5, in
print(float(Percent(12)))
~~~~~~~^^^^
File "", line 3, in __init__
super().__init__(x / 100)
~~~~~~~~~~~~~~~~^^^^^^^^^
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
Код: Выделить всё
class MyFloat:
def __init__(self, value):
self.value = value
def __float__(self):
return self.value
class Percent(MyFloat):
def __init__(self, x):
super().__init__(x / 100)
print(float(Percent(12)))
# 0.12
- Почему я не могу успешно наследовать float?
- Есть ли способы обойти эту проблему, кроме самостоятельного определения класса MyFloat?