|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这几天学习类与对象,晕乎乎的
- ###########################################P67 对象与类
- class S(str):
- def __add__(self,other):
- print('qq',self,other)
- return len(self)+len(other)
- s1=S('Fishc')
- s2=S('ee')
- s3=S('fff')
- print(s1+s2)
- class S1(str):
- def __add__(self, other):
- return NotImplemented
- class S2(str):
- def __radd__(self, other):
- return len(self)+len(other)
- s1=S1('Apple')
- s2=S2('Banana')
- print(s1+s2)
- class S1(str):
- def __iadd__(self, other):
- return len(self)+len(other)
- s1=S1('Apple')
- s1+=s2
- print(s1)
- s2+=s2
- print(s2)
- ###########################################P66 对象与类
- print(bin(22))
- print(~22)
- print(9>>2)
- print(0.1+0.2)
- class C:
- def __index__(self):
- print('拦截')
- return 3
- c=C()
- # print(list(c))
- s='FishC'
- print(s[c])
- ##################
- class C:
- def __getitem__(self, index):
- print(index)
- c=C()
- c[2]
- c[2:8]
- s='I love FishC'
- print(s[2:6])
- print(s[slice(2,6)])
- print(s[7:])
- print(s[slice(7,None)])
- print(s[::4])
- print(s[slice(None,None,4)])
- class D:
- def __init__(self,data):
- self.data=data
- def __getitem__(self, index):
- return self.data[index]
- def __setitem__(self, index, value):
- self.data[index]=value
- d=D([1,2,3,4,5])
- print(d[1])
- d[1]=1
- d[2:4]=[2,3]
- print(d[:])
- class D:
- def __init__(self,data):
- self.data=data
- def __getitem__(self, index):
- return self.data[index]*2
- d=D([1,2,3,4,5])
- for i in d:
- print(i,end=' ')
- class Double:
- def __init__(self,start,stop):
- self.value = start-1
- self.stop=stop
- def __iter__(self):
- return self
- def __next__(self):
- if self.value ==self.stop:
- raise StopIteration
- self.value +=1
- return self.value*2
- d =Double(1,5)
- for i in d:
- print(i,end=' ')
复制代码 |
|