diff options
-rw-r--r-- | LectureNotesOnPython.rst | 230 |
1 files changed, 223 insertions, 7 deletions
diff --git a/LectureNotesOnPython.rst b/LectureNotesOnPython.rst index fac7727..6abf332 100644 --- a/LectureNotesOnPython.rst +++ b/LectureNotesOnPython.rst @@ -1645,8 +1645,11 @@ copy 拷贝类中的属性, 但不会重新制作一个对象中的对象 (e 纯函数与修改函数 ```````````````````````````````````````````````````` -.. code:: python +纯函数(pure function)。 + +修改函数(modifier function)。 +.. code:: python class Time: def __init__(self, h, m, s): @@ -1654,28 +1657,241 @@ copy 拷贝类中的属性, 但不会重新制作一个对象中的对象 (e self.minute = m self.second = s + def __str__(self): + return '%02d:%02d:%02d' % (self.hour, self.minute, self.second) + + def time_to_seconds(time): + minutes = time.hour * 60 + time.minute + seconds = minutes * 60 + time.second + return seconds + + def seconds_to_time(seconds): + time = Time(0, 0, 0) + minutes, time.second = divmod(seconds, 60) + time.hour, time.minute = divmod(minutes, 60) + return time + def increment_time_pure(t, h, m, s): ''' Pure function ''' - t2 = Time(0,0,0) - return t2 - + secs = time_to_seconds(t) + time_to_seconds(Time(h, m, s)) + return seconds_to_time(secs) + + def increment_time_modifier(t, h, m, s): ''' Modifier function ''' - t2 = Time(0,0,0) - return t2 + t.hour += h + t.minute += m + t.second += s + + def increment_time_modifier2(t, h, m, s): + ''' Modifier function ''' + t.second += s + if t.second > 59: + minute_from_seconds, t.second = divmod(t.second, 60) + t.minute += m + minute_from_seconds + if t.minute > 59: + hour_from_minutes, t.minute = divmod(t.minute, 60) + t.hour += h + hour_from_minutes + + t = Time(6, 58, 55) + print(t) ti = increment_time_pure(t, 2, 30, 50) + print(ti) + increment_time_modifier(t, 2, 30, 50) + print(t) + print(seconds_to_time(time_to_seconds(t))) + + +**Designed development** . 先有总体设计,然后再有开发。比如先把时间转成秒,再把秒转成时间(hh:mm:ss)。 + +**Prototype and patch** . 先造个原型,如果有问题,逐步修改。 + +类中的方法,就是在类中的函数定义。对方法的调用与对函数的调用不同。 对方法的调用我们用点操作符(dot notation)。点前面是对象,点后面是方法。 + + +调用类方法 +```````````````````````````````````````````````````` + +.. code:: python + + class Time: + def __init__(self, h, m, s): + self.hour = h + self.minute = m + self.second = s + + def print_time(self): + print( '%02d:%02d:%02d' % (self.hour, self.minute, self.second) ) + + def print_time2(time): + print( '%02d:%02d:%02d' % (time.hour, time.minute, time.second) ) + + + t = Time(1, 30, 20) + Time.print_time(t) + Time.print_time2(t) + t.print_time() + t.print_time2() + + +练习:在Time类中添加一个方法 time_to_seconds,把时间转换成秒。 + +练习:在Time类中添加一个方法, is_after, 返回True/False。 + +练习:在Time类中添加一个方法, __add__, 使得我们可以用加号相加两个时间。 如果是一个时间加上一个整数呢? 或者是一个整数加上一个时间呢? 提示:使用isinstance,__radd__。 + + +方法中可以定义函数 +`````````````````````````````````````````````````````` + +.. code:: python + + class Time: + def __init__(self, h, m, s): + self.hour = h + self.minute = m + self.second = s + + def time_to_seconds(self): + minutes = self.hour * 60 + self.minute + seconds = minutes * 60 + self.second + return seconds + + def increment_time(self, seconds): + + def seconds_to_hhmmss(seconds): + minutes, seconds = divmod(seconds, 60) + hours, minutes = divmod(minutes, 60) + return hours, minutes, seconds + + self.hour, self.minute, self.second = seconds_to_hhmmss(self.time_to_seconds() + seconds) + + def __str__(self): + return '%02d:%02d:%02d' % (self.hour, self.minute, self.second) + + + + t = Time(1, 30, 20) + print(t) + t.increment_time(3600) + print(t) + + +在t.increment_time(3600)中, t叫做subject。 + +接口与实现相分离。 + +Positional argument. Keyword argument. + +.. code:: python + + + class Time: + def __init__(self, h, m=0, s=0): + self.hour = h + self.minute = m + self.second = s + + def __str__(self): + return '%02d:%02d:%02d' % (self.hour, self.minute, self.second) + + + t = Time(1) + print(t) + t = Time(1, s=20) + print(t) + t = Time(1, m=30) + print(t) + t = Time(1, 30, 20) + print(t) + + + +多态 (polymorphism) +``````````````````````````````````````````````````` + +如果函数可以对多个类型使用,我们把它叫做多态函数。 如histogram,可以对字符串使用去数里面字符的频率, 也可以对列表使用去数里面元素的频率。 + +如系统自带的sum函数。 sum([1, 2, 3]), sum([Time(1,0,0), Time(0,1,0), Time(0,0,1)])。 + + +.. code:: python + + def time_to_seconds(time): + minutes = time.hour * 60 + time.minute + seconds = minutes * 60 + time.second + return seconds + + def seconds_to_time(seconds): + time = Time(0, 0, 0) + minutes, time.second = divmod(seconds, 60) + time.hour, time.minute = divmod(minutes, 60) + return time + class Time: + def __init__(self, h=0, m=0, s=0): + self.hour = h + self.minute = m + self.second = s + + def __add__(self, other): + if isinstance(other, Time): + return seconds_to_time( time_to_seconds(self) + time_to_seconds(other) ) + else: + return seconds_to_time( time_to_seconds(self) + other ) + + def __radd__(self, other): + return self.__add__(other) + + def __str__(self): + return '%02d:%02d:%02d' % (self.hour, self.minute, self.second) + + + print(sum([Time(1,0,30), Time(0,0,30), Time(0,59,0)])) + + + +继承(inheritance) +`````````````````````````````````````````````````` + +继承父类中的属性与方法。 + +.. code:: python + + class A: + def __init__(self, a): + self.a = a + def __str__(self): + return 'Parent string method.' + + class B(A): + pass + + + class C(A): + def __init__(self, a, b): + super().__init__(a) + self.b = b + + + x = B(2) + print(x) + y = C(2, 3) + print(y) + + +扑克牌。Card, Deck, Hand(Deck)。 web 应用程序 ```````````` -输入文本 +在文本框输入文本 `````````````````````````````````````````````````` school.py |