#9.2.1重写一般方法和特殊方法classA:defhello(Self):print"A"classB(A):pass
a=A()
b=B()
a.hello() #结果 A
b.hello() #结果 AclassB(A):#简单重写defhello(self):print"B"
b=B()
b.hello() #结果 B#特殊方法classbird:def__init__(self):
self.hungry = True
self.thirsty = True
self.insky = Falsedefeat(self):if (self.hungry == True):
print"I am eating! Aaaaaaaaaaah~"
self.hungry = Falseelse:
print"I am full! I don not want to eat anything!"defdrink(self):if (self.thirsty == True):
print"I am drinking water. Gu lu, Gu lu, Gu lu~"
self.thirsty = Falseelse:
print"Too much water! Now I do not need water."deftakeoff(self):if (self.hungry == True):
print"I am too hungry to fly. Please run bird.eat()"elif (self.thirsty == True):
print"I am too thirsty to fly. Please run bird.drink()"else:
if(self.insky == False):
print"I am ready to fly! Takeoff now! Pu pu pu pu ~"
self.insky = Trueelse:
print"I am already in the sky! Wow!"deflanding(self):if (self.insky == True):
print"I am tired. Now I am going to takeoff."
self.insky = Falseelse:
print"I have already on the land."defsing(self):print"Squawk! Squawk! Squawk!"
b = bird()
b.takeoff()#结果 I am too hungry to fly. Please run bird.eat()
b.eat()
classBird:def__init__(self):
self.hungry=Truedefeat(self):if self.hungry:
print"Aaaah"
self.hungry=Falseelse:
print"No"
c=Bird()
c.eat() #结果 Aaaah
c.eat() #结果 NoclassSongBird(Bird):def__init__(self):
self.sound="Squawk"defsing(self):print self.sound
sc=SongBird()
sc.sing() #结果 Squawk#9.2.2调用未绑定的超类构造方法classSongBird(Bird):def__init__(self):
Bird.__init__(self)
self.sound="Squawk"defsing(self):print self.sound
sc=SongBird()
sc.sing() #结果 Squawk
sc.eat()#结果Aaaah
sc.eat()#结果 No#9.2.3使用super函数
__metaclass__=type #super只在新式类中起作用classBird:def__init__(self):
self.hungry=Truedefeat(self):if self.hungry:
print"Aaaah"
self.hungry=Falseelse:
print"No"classSongBird(Bird):def__init__(self):
super(SongBird,self).__init__()
self.sound="Squawk"defsing(self):print self.sound
sc=SongBird()
sc.sing() #结果 Squawk
sc.eat()#结果Aaaah
sc.eat()#结果 No
#9.6.1迭代器规则classFibs:
f __init__(self):
self.a=0
self.b=1defnext(self):
self.a,self.b=self.b,self.a+self.b
return self.a
def__iter__(self):return self
fibs=Fibs()
for f in fibs:
if f>1000:
print f #结果1597break#9.6.2从迭代器得到序列classTestlterator:
value=0defnext(self):
self.value+=1if self.value>10:raise StopIteration
return self.value
def__iter__(self):return self
ti=Testlterator()
print list(ti) #结果[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
9.7生成器
#9.7.1创建生成器defflatten(nested):for sublist in nested:
for element in sublist:
yield element
nested=[[1,2],[3,4],[5]]
for num in flatten(nested):
print num
'''
1
2
3
4
5
'''print list(flatten(nested)) #结果[1, 2, 3, 4, 5]#9.7.2递归生成器defflatten(nested):try:
for sublist in nested:
for element in flatten(sublist):
yield element
except TypeError:
yield nested
print list(flatten([[1,2],[3,4],[5]])) #结果[1, 2, 3, 4, 5]
9.8八皇后问题
#定义冲突defconflict(state,nextX):
nextY=len(state)
for i in range(nextY):
if abs(state[i]-nextX) in (0,nextY-i):
returnTruereturnFalse#放置最后一个皇后defqueens(num=8,state=()):for pos in range(num):
ifnot conflict(state,pos):
if len(state)==num-1:
yield (pos,)
#需要递归的情况 else:
for result in queens(num,state+(pos,)):
yield(pos,)+result
#print list(queens(4,(1,3,0))) #测试queen和conflict 输出2print list(queens(3)) #结果 []print list(queens(4)) #结果 [(1, 3, 0, 2), (2, 0, 3, 1)]defprettyprint(solution):defline(pos,length=len(solution)):return"+"*(pos)+"X"+"+"*(length-pos-1)
for pos in solution:
print line(pos)
import random
prettyprint(random.choice(list(queens(8))))
'''
+++X++++
X+++++++
++++X+++
+++++++X
+X++++++
++++++X+
++X+++++
+++++X++
'''