第二章答案
本章答案需要配合使用教材中例题的源代码
可以自己手打一遍一可以在网上下载使用,也可以在我分享的资源中下载
需要将下载的code放在合适的位置
例:
from TheCode.ch02.progressions import Progression
我将源代码放在了本代码目录下TheCode中,使用了其中的ch02中的progressions
- 2.4答案
#2.4
class Flower(object):
'''
name num price
change_name change_num change_price
'''
def __init__(self,name,num,price):
self.name = name
self.num = num
self.price = price
def change_name(self,name):
self.name = name
def change_num(self,num):
self.num = num
def change_price(self,price):
self.price = price
def show(self):
print("这是%s,还有%d支,每支%d元"%(self.name,self.num,self.price))
if __name__ == '__main__':
f = Flower('康乃馨',10,5)
f.show()
f.change_num(100)
f.show()
print(f.num)
- 2.5 ~2.8答案
from TheCode.ch02.credit_card import CreditCard
#import /TheCode/ch02/creat_card.py
class SubCreditCard(CreditCard):
def __init__(self, customer, bank, acnt, limit,plus=None):
if plus:
self.plus = plus
else :
self.plus = None
super().__init__(customer, bank, acnt, limit)
def charge(self,price):
try:
if price+self._balance > self._limit:
return False
else:
self._balance+=price
except :
print('pleace input a num')
def make_payment(self,amount):
try:
self._balance-=amount
except:
print('pleace input a num')
quit()
if amount<0:
raise ValueError
def get_accountp(self):
if self.plus:
return self.plus
else:
print('No one in our accountr')
if __name__=="__main__":
sub = SubCreditCard('John Bowman', 'California Savings',
'5391 0375 9387 5309', 2500,666)
print(sub.get_balance())
sub.make_payment(1)
print(sub.get_balance()) #2.6
print(sub.get_accountp()) #2.7
#2.8 第三个率先达到limit 他们以1:2:3的速率增长,就需要1:2:3的空间
- 2.9~2.15
from TheCode.ch02.vector import Vector
class PredatoryVector(Vector):
'''
d is a len of list or a list
'''
def __init__(self,d):
super().__init__(d)
#2.9
def __sub__(self,other):
if len(self)!=len(other):
raise ValueError('dimensions must agree')
else:
try:
result=PredatoryVector([self[i]-other[i] for i in range(len(self))])
return result
except:
raise TypeError('the list must be num')
#2.11
def __radd__(self,other):
if len(self)!=len(other):
raise ValueError('dimensions must agree')
else:
try:
result=PredatoryVector([self[i]+other[i] for i in range(len(self))])
return result
except:
raise TypeError('the list must be num')
def get_vector(self):
return self._coords
#2.12
def __mul__(self,d):
if type(d)==int:
try:
mid = self.get_vector()
for i in range(d-1):
self+=mid
return self
except:
print('your input is wrong')
else:
try:
result = PredatoryVector(len(d))
for i in range(len(d)):
result[i]=self[i]*d[i]
return result
except:
print('your input is wrong')
raise TypeError
#2.13
def __rmul__(self,d):
try:
mid = self.get_vector()
for i in range(d-1):
self+=mid
return self
except:
print('your input is wrong')
if __name__=="__main__":
#2.9
p1=PredatoryVector((1,2,3))
p2=PredatoryVector([4,5,6])
print('p1 is :',p1)
print('p2 is :',p2)
print('#2.9 p2-p1:',p2-p1)
#2.10 已经实现
print('#2.10 -p1:',-p1)
#2.11
print('#2.11 [4,5,6]+p1:',[4,5,6]+p1)
#2.12
print('#2.12 p1*3:',p1*3)
#2.13
print('#2.13 3*p1:',3*p1)
#2.14
print('#2.14 p1*p2:',p1*p2)
#2.15
print('题目要求求改构造函数,但是构造函数看起来不需要改')
- 2.16~2.21
from TheCode.ch02.progressions import Progression
#2.18
class FobonacciProgression(Progression):
def __init__(self,first,second):
super().__init__(first)
self._prev = second-self._current
def _advance(self):
self._prev,self._current = self._current,self._current+self._prev
if __name__=="__main__":
#2.16 self._length = max(0, (stop - start + step - 1) // step)
print('#2.16 : 列表长度: -1是range后一个元素不算,+step是让值算上第一个数')
#2.18
fb = FobonacciProgression(0,2)
print('#2.18 ',end='')
fb.print_progression(8)
#2.19
print('#2.19 128是2的8次方,要达到2的63次方需要2的55方个128相加')
#2.20
print('#2.20 当继承数特别深使,1将每个类中的变量加入到一个实例空间,2每个类会创建各自的类命名空间')
#2.21
print('#2.21 可能太多代码重复编辑,使代码臃肿,降低可重用行')
- 2.22~2.23
from TheCode.ch02.sequence_abc import Sequence
class PredatorySequence(Sequence):
def __init__(self,arm):
self._arm = arm
def __len__(self):
len(self._arm)
def __getitem__(self,val):
return arm[val-1]
#2.22
def __eq__(self,other):
if self._arm==other._arm:
return True
else:
return False
def __lt__(self,other):
if len(self) > len(other):
return True
else:
return False
if __name__=="__main__":
pse1=PredatorySequence('1,2,3')
pse2=PredatorySequence('1,3,3')
#2.22
print(pse1==pse2)
#2.23
print("比较字典值长度的大小")
- 2.26
from TheCode.ch02.sequence_iterator import SequenceIterator
from TheCode.ch02.range import Range
class ReversdSequece(SequenceIterator):
def __init__(self,sequence):
super().__init__(sequence)
self._k = -1
def __next__(self):
if -(self