鱼C论坛

 找回密码
 立即注册
查看: 2633|回复: 13

[作品展示] 重写 str

[复制链接]
发表于 2020-4-29 11:17:38 | 显示全部楼层 |阅读模式
本帖最后由 _2_ 于 2020-5-1 09:10 编辑

上代码:

  1. class MyStr(str):
  2.     """重写 str"""
  3.     def __lshift__(self, other: int):
  4.         """
  5.         Return self << other.
  6.         """
  7.         if not isinstance(other, int):
  8.             raise ValueError("'other' must be an integer, not other types.")
  9.         _self = self[other:len(self) - 1]
  10.         return MyStr(_self)

  11.     def __rshift__(self, other: int):
  12.         """
  13.         Return self >> other.
  14.         """
  15.         if not isinstance(other, int):
  16.             raise ValueError("'other' must be an integer, not other types.")
  17.         _self = self[0:other]
  18.         return MyStr(_self)

  19.     def __str__(self):
  20.         """
  21.         Return str(self)
  22.         """
  23.         return str.__str__(self)

  24.     def __sub__(self, other):
  25.         """
  26.         Return self - other.
  27.         """
  28.         if other in self:
  29.             raise ValueError("'other' not in 'self'")
  30.         else:
  31.             _self = self.split(other)
  32.             return MyStr("".join(_self))
复制代码


还有一件事,
我在想 __rlshift__() 和 __rrshift__() 该怎么写,
如果你有想法,欢迎在候选中选择一项,
投票结束后我会参考票数最多的进行编写,
欢迎大家积极投票

选项1:__rlshift__(): return other << self ; __rrshift__(): return other >> self
选项2:__rlshift__(): return self >> other ; __rrshift__(): return self << other
单选投票, 共有 17 人参与投票
您所在的用户组没有投票权限

评分

参与人数 2荣誉 +2 鱼币 +7 贡献 +2 收起 理由
WangJS + 2 + 2 + 2 虽然看不懂……
v.ki + 5

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-29 11:18:35 | 显示全部楼层
沙发
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-29 11:23:12 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-29 11:24:23 | 显示全部楼层
当然是2
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-29 11:28:47 | 显示全部楼层
你只能选2,因为1是错的。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-29 12:42:16 From FishC Mobile | 显示全部楼层
永恒的蓝色梦想 发表于 2020-4-29 11:28
你只能选2,因为1是错的。


不合逻辑的 1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-30 15:18:43 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-1 09:10:34 From FishC Mobile | 显示全部楼层
顶……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-1 16:44:48 From FishC Mobile | 显示全部楼层
再顶……
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-6 12:51:26 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-6 13:37:51 | 显示全部楼层
帮楼主顶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-5-7 11:58:24 From FishC Mobile | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-26 18:03:37 | 显示全部楼层
猜对了
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-6-20 11:50:17 | 显示全部楼层
顶顶顶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-30 13:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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