class duck():
def walk(self):
print('I walk like a duck')
def swim(self):
print('i swim like a duck')
class person():
def walk(self):
print('this one walk like a duck')
def swim(self):
print('this man swim like a duck')
def watch_duck(animal):
animal.walk()
animal.swim()
small_duck = duck()
watch_duck(small_duck)
output >>
I walk like a duck
i swim like a duck
duck_like_man = person()
watch_duck(duck_like_man)
output >>
this one walk like a duck
this man swim like a duck
class Lame_Foot_Duck():
def swim(self):
print('i am lame but i can swim')
lame_duck = Lame_Foot_Duck()
watch_duck(lame_duck)
output >>
AttributeError: Lame_Foot_Duck instance has no attribute 'walk'
class CollectionClass():
lists = [1,2,3,4]
def __getitem__(self, index):
return self.lists[index]
iter_able_object = CollectionClass()
class Another_iterAbleClass():
lists=[1,2,3,4]
list_position = -1
def __iter__(self):
return self
def next(self): #还有更简单的实现,使用生成器或迭代器什么的:)
self.list_position += 1
if self.list_position >3:
raise StopIteration
return self.lists[self.list_position]
another_iterable_object=Another_iterAbleClass()
print(iter_able_object[1])
print(iter_able_object[1:3])
output>>
2
[2, 3]
another_iterable_object[2]
output>>
Traceback (most recent call last):
File "/Users/steinliber/a.py", line 32, in <module>
another_iterable_object[2]
TypeError: 'Another_iterAbleClass' object does not support indexing
print(next(another_iterable_object))
output>>
1
print(next(another_iterable_object))
output>>
2
print(next(iter_able_object))
output>>
Traceback (most recent call last):
File "/Users/steinliber/a.py", line 29, in <module>
print(next(iter_able_object))
TypeError: IterAbleClass object is not an iterator
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有