新手求助
class Nstr(str):def __lshift__(self, other):
return self + self[:other]
def __rshift__(self, other):
return self[-other:] + self[:-other]
self+self[:other]
self[-other:]+self[:-other]是啥意思{:10_249:} 切片 + 拼接{:10_264:} 一个是切片,一个是字符串的拼接 本帖最后由 Hoiste 于 2020-3-6 20:39 编辑
举个例子吧,比如传入字符串string = Nstr(fishC.com),定义了移位操作,如string << 3就会触发左移位运算,string作为self,3作为other传入def __lshift__(self, other),代入进去返回的结果就是就是return self + self[:other],代入已知,string即string,string[:other]即string[:3],string代表字符串序列fishC,结果就是'hC' + 'fis'得到‘hCfish’,也就是左移了3位,另一个也是同理的
页:
[1]