鱼C论坛

 找回密码
 立即注册
查看: 4397|回复: 0

[学习笔记] 2022-10-19 学习Python-Day19

[复制链接]
发表于 2022-10-19 17:16:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这几天学习类与对象,晕乎乎的
  1. ###########################################P67 对象与类
  2. class S(str):
  3.     def __add__(self,other):
  4.         print('qq',self,other)
  5.         return len(self)+len(other)
  6. s1=S('Fishc')
  7. s2=S('ee')
  8. s3=S('fff')
  9. print(s1+s2)
  10. class S1(str):
  11.     def __add__(self, other):
  12.         return NotImplemented
  13. class S2(str):
  14.     def __radd__(self, other):
  15.         return len(self)+len(other)
  16. s1=S1('Apple')
  17. s2=S2('Banana')
  18. print(s1+s2)
  19. class S1(str):
  20.     def __iadd__(self, other):
  21.         return len(self)+len(other)
  22. s1=S1('Apple')
  23. s1+=s2
  24. print(s1)
  25. s2+=s2
  26. print(s2)
  27. ###########################################P66 对象与类
  28. print(bin(22))
  29. print(~22)
  30. print(9>>2)
  31. print(0.1+0.2)
  32. class C:
  33.     def __index__(self):
  34.         print('拦截')
  35.         return 3
  36. c=C()
  37. # print(list(c))
  38. s='FishC'
  39. print(s[c])
  40. ##################
  41. class C:
  42.     def __getitem__(self, index):
  43.         print(index)
  44. c=C()
  45. c[2]
  46. c[2:8]
  47. s='I love FishC'
  48. print(s[2:6])
  49. print(s[slice(2,6)])
  50. print(s[7:])
  51. print(s[slice(7,None)])
  52. print(s[::4])
  53. print(s[slice(None,None,4)])
  54. class D:
  55.     def __init__(self,data):
  56.         self.data=data
  57.     def __getitem__(self, index):
  58.         return self.data[index]
  59.     def __setitem__(self, index, value):
  60.         self.data[index]=value
  61. d=D([1,2,3,4,5])
  62. print(d[1])
  63. d[1]=1
  64. d[2:4]=[2,3]
  65. print(d[:])
  66. class D:
  67.     def __init__(self,data):
  68.         self.data=data
  69.     def __getitem__(self, index):
  70.         return self.data[index]*2
  71. d=D([1,2,3,4,5])
  72. for i in d:
  73.     print(i,end=' ')
  74. class Double:
  75.     def __init__(self,start,stop):
  76.         self.value = start-1
  77.         self.stop=stop
  78.     def __iter__(self):
  79.         return self
  80.     def __next__(self):
  81.         if self.value ==self.stop:
  82.             raise StopIteration
  83.         self.value +=1
  84.         return self.value*2
  85. d =Double(1,5)
  86. for i in d:
  87.     print(i,end=' ')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-23 02:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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