#工厂模式
class BMW():
def init(self,name):
self.name=name
class Benz():
def init(self,name):
self.name=name
class CarFactory():
@staticmethod
def makecar(name):
if name==‘宝马’:
return BMW(name)
elif name==‘奔驰’:
return Benz(name)
car=CarFactory.makecar(‘宝马’)#car 是最终返回的类的对象
print(car.name,type(car))
car2=CarFactory.makecar(‘奔驰’)
print(car2.name,type(car2))
def fbnq(n):
a,b=0,1
i=0
while i<n:
a,b=b,a+b
yield a
i+=1
y=fbnq(10)
#for x in fbnq(10):
print(x)
while True:
try:
print(next(y))
except:
break
y=(i for i in range(6))#生成器表达式
print(y)
print(True +4)# True 是1
print(False +9)#Flase 是0