8.5.1 很多人都把眼光放在了面向对象上
python同时支持鸭子类型和面向对象, 很多人把眼光放在了面向对象上。
8.5.2 python用法简单的根本 - 鸭子类型(基于协议编程)
from collections.abc import Iterable
class Company:
def __init__(self, employee_list):
self.employee = employee_list
def __iter__(self):
return iter(self.employee)
def __getitem__(self, item):
return self.employee[item]
if __name__ == "__main__":
company = Company(["tom", "bob", "jane"])
if isinstance(company, Iterable):
print("company是iterable类型")
for item in company:
print (item)
a = []
if isinstance(a, Iterable):
print("yes")