需要重载+操作符,参数:Convert or int,怎么重载最好?
class Convert:
'''Класс для конвертации десятичных чисел в бинарные'''
def __init__(self, dec: int) -> None:
self.__dec: int = dec
def __str__(self) -> str:
return f'{self.__dec}'
def __add__(self, other: t.Union['Convert', int]) -> 'Convert':
if isinstance(other, Convert):
return Convert(self.__dec + other.__dec)
return Convert(self.__dec + other)
在你的情况下,使用是
isinstance好的。有一个与魔法方法相关的漏洞
__int__。定义您的类型到整数的转换。它不适用于其他类型: