|
本帖最后由 _2_ 于 2020-5-1 09:10 编辑
上代码:
- class MyStr(str):
- """重写 str"""
- def __lshift__(self, other: int):
- """
- Return self << other.
- """
- if not isinstance(other, int):
- raise ValueError("'other' must be an integer, not other types.")
- _self = self[other:len(self) - 1]
- return MyStr(_self)
- def __rshift__(self, other: int):
- """
- Return self >> other.
- """
- if not isinstance(other, int):
- raise ValueError("'other' must be an integer, not other types.")
- _self = self[0:other]
- return MyStr(_self)
- def __str__(self):
- """
- Return str(self)
- """
- return str.__str__(self)
- def __sub__(self, other):
- """
- Return self - other.
- """
- if other in self:
- raise ValueError("'other' not in 'self'")
- else:
- _self = self.split(other)
- return MyStr("".join(_self))
复制代码
还有一件事,
我在想 __rlshift__() 和 __rrshift__() 该怎么写,
如果你有想法,欢迎在候选中选择一项,
投票结束后我会参考票数最多的进行编写,
欢迎大家积极投票
选项1:__rlshift__(): return other << self ; __rrshift__(): return other >> self
选项2:__rlshift__(): return self >> other ; __rrshift__(): return self << other |
评分
-
查看全部评分
|