|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
[课后作业] 第042讲 中的有的继承int,有的继承str,有的又不继承
https://fishc.com.cn/forum.php?m ... peid%26typeid%3D398
- 第一段代码:
- class Nstr(str):
- def __lshift__(self, other):
- return self[other:] + self[:other]
- def __rshift__(self, other):
- return self[-other:] + self[:-other]
- 第二段代码:
- class Nstr:
- def __init__(self, arg=''):
- if isinstance(arg, str):
- self.total = 0
- for each in arg:
- self.total += ord(each)
- else:
- print("参数错误!")
- def __add__(self, other):
- return self.total + other.total
- def __sub__(self, other):
- return self.total - other.total
- def __mul__(self, other):
- return self.total * other.total
- def __truediv__(self, other):
- return self.total / other.total
- def __floordiv__(self, other):
- return self.total // other.total
- 第三段代码:
- class Nstr(int):
- def __new__(cls, arg=0):
- if isinstance(arg, str):
- total = 0
- for each in arg:
- total += ord(each)
- arg = total
- return int.__new__(cls, arg)
复制代码 |
|