16.1 time
16.2pure functions
编写两个函数,实现时间相加功能。
class Time(object): '''指代时间 属性:hour,minute,second''' time = Time() def add_time(t1,t2): sum = Time() sum.hour = t1.hour + t2.hour sum.minute = t1.minute + t2.minute sum.second = t1.second + t2.second
if sum.second >= 60:
sum.second -= 60
sum.minute += 1
if sum.minute >= 60:
sum.minute -= 60
sum.hour += 1 return sum def print_time(time): print("%.2d"% time.hour +":" + "%.2d"% time.minute + ":" + "%.2d"% time.second)
测试:
>>> s = Time() >>> s.hour = 9 >>> s.minute = 45 >>> s.second = 0 >>> dur = Time() >>> dur.hour = 1 >>> dur.minute = 35 >>> dur.second = 0 >>> done = add_time(s,dur) >>> print_time(done) 11:20:00
16.3modifiers
16.4prototyping versus planning(原型设计与计划)
其实就是将问题普遍化看似将问题复杂化,有时却简化了问题(因为特殊情况变少,出错概率也降低)