Python自定义字符串类实现加减法运算
class MyStr(str): def add(self, other): res = self for c in other: if c not in self: res += c return res
def __sub__(self, other):
res = ""
for c in self:
if c not in other:
res += c
return res
s1 = MyStr('abcdefg') s2 = MyStr('acdi') print(s1 + s2) # 'abcdefgi' print(s1 - s2) # 'bcef'
原文地址: https://www.cveoy.top/t/topic/nZGB 著作权归作者所有。请勿转载和采集!