2022-10-19 学习Python-Day19
这几天学习类与对象,晕乎乎的{:10_283:}###########################################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)
##################
class C:
def __getitem__(self, index):
print(index)
c=C()
c
c
s='I love FishC'
print(s)
print(s)
print(s)
print(s)
print(s[::4])
print(s)
class D:
def __init__(self,data):
self.data=data
def __getitem__(self, index):
return self.data
def __setitem__(self, index, value):
self.data=value
d=D()
print(d)
d=1
d=
print(d[:])
class D:
def __init__(self,data):
self.data=data
def __getitem__(self, index):
return self.data*2
d=D()
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=' ')
页:
[1]