Код: Выделить всё
class Timestamp(int):
def as_seconds(self):
return self / 90000
Код: Выделить всё
>>> ts1 = Timestamp(135000)
>>> ts1
135000
>>> ts1.as_seconds()
1.5
Код: Выделить всё
>>> ts2 = ts1 + 22500
>>> ts2
157500
>>> ts2.as_seconds()
Traceback (most recent call last):
File "
", line 1, in
ts2.as_seconds()
^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'as_seconds'
Я создаю подкласс int, чтобы наследовать арифметические методы (
Код: Выделить всё
__add__Подробнее здесь: https://stackoverflow.com/questions/798 ... rived-type
Мобильная версия