鱼C论坛

 找回密码
 立即注册
查看: 2516|回复: 1

[已解决]python 课后作业42讲动动手2的右移为什么不右移

[复制链接]
发表于 2016-2-20 16:11:39 | 显示全部楼层 |阅读模式
1鱼币
class Nstr(str):
    def __lshift__(self,other):
        return self[other:] + self[:other]

    def __rshift__(self,other):
        return self[:-other] + self[-other:]


>>> a = Nstr('xiao jia yu!')
>>> a<<3
'o jia yu!xia'
>>> a>>2
'xiao jia yu!'
>>>
为什么他不右移???
最佳答案
2016-2-20 16:11:40
因为你的__rshift__()方法定义错误
正确的定义:
  1. class Nstr(str):
  2.     def __lshift__(self,other):
  3.         return self[other:] + self[:other]

  4.     def __rshift__(self,other):
  5.         return self[-other:] + self[:-other]

  6. >>> a = Nstr('xiao jia yu!')
  7. >>> a<<3
  8. 'o jia yu!xia'
  9. >>> a>>3
  10. 'yu!xiao jia '
复制代码

最佳答案

查看完整内容

因为你的__rshift__()方法定义错误 正确的定义:
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-2-20 16:11:40 | 显示全部楼层    本楼为最佳答案   
因为你的__rshift__()方法定义错误
正确的定义:
  1. class Nstr(str):
  2.     def __lshift__(self,other):
  3.         return self[other:] + self[:other]

  4.     def __rshift__(self,other):
  5.         return self[-other:] + self[:-other]

  6. >>> a = Nstr('xiao jia yu!')
  7. >>> a<<3
  8. 'o jia yu!xia'
  9. >>> a>>3
  10. 'yu!xiao jia '
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-10-14 03:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表