summaryrefslogtreecommitdiff
path: root/LectureNotesOnPython.rst
diff options
context:
space:
mode:
authorHui Lan <lanhui@zjnu.edu.cn>2019-04-29 20:55:22 +0800
committerHui Lan <lanhui@zjnu.edu.cn>2019-04-29 20:55:22 +0800
commitbbac0a495325521de31ae0382f49fc85d2a6f713 (patch)
tree67868dcfba20b4d0aea21dacfc1338b83397ab18 /LectureNotesOnPython.rst
parent2e1cab9bbb4aea8ddadef5b60ff197fcb9616f9b (diff)
加入类Point, Rectangle, Time
Diffstat (limited to 'LectureNotesOnPython.rst')
-rw-r--r--LectureNotesOnPython.rst153
1 files changed, 152 insertions, 1 deletions
diff --git a/LectureNotesOnPython.rst b/LectureNotesOnPython.rst
index 36a1eb3..fac7727 100644
--- a/LectureNotesOnPython.rst
+++ b/LectureNotesOnPython.rst
@@ -1520,9 +1520,160 @@ Memo
类 class 与 对象 object
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+程序员用类(class)来定义一类数据。
+
+.. code:: python
+
+ class Point:
+ """Represents a point in 2-D space."""
+
+
+ blank = Point()
+
+ blank.x = 1 # an attribute
+ blank.y = 2 # an attribute
+ x = blank.x
+
+
+ def print_point(p):
+ print('(%g, %g)' % (p.x, p.y))
+
+ def reset_point(p): # object is Mutable
+ p.x = 0
+ p.y = 0
+
+ print_point(blank)
+ reset_point(blank)
+ print_point(blank)
+
+
+blank 是(指向) Point类型的对象的一个变量。 注意到Point后面跟随了括
+号, 是对类的实例化 (instantiation)。 我们说 blank 是Point类的一个实
+例 (instance), 也可以说是Point类的一个对象。
+
+对象是Mutable类型数据。 如 reset_point 所示。
+
+
+一个对象可以是另外一个对象的属性
+````````````````````````````````````````````````````
+
+.. code:: python
+
+ class Point:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __str__(self):
+ return '(%g, %g)' % (self.x, self.y)
+
+
+ class Rectangle:
+ def __init__(self, lower_left_corner, width, height):
+ self.lower_left_corner = lower_left_corner # lower_left_corner is a Point object
+ self.width = width
+ self.height = height
+
+ def get_center(self):
+ return Point(self.lower_left_corner.x + self.width/2, self.lower_left_corner.y + self.height/2)
+
+
+ class Circle:
+ def __init__(self, center, radius):
+ self.center = center # center is a Point object
+ self.r = radius
+
+
+ r = Rectangle(Point(0,0), 10, 4)
+ print(r.get_center())
+
+
+
+copy 与 deepcopy
+````````````````````````````````````````````````````
+
+copy 拷贝类中的属性, 但不会重新制作一个对象中的对象 (embedded object),而是拷贝其引用而已。 而deepcopy会。
+
+
+.. code:: python
+
+
+ import copy
+
+ class Point:
+ def __init__(self, x, y):
+ self.x = x
+ self.y = y
+
+ def __str__(self):
+ return '(%g, %g)' % (self.x, self.y)
+
+ def __eq__(self, other):
+ return self.x == other.x and self.y == other.y
+
+
+ class Rectangle:
+ def __init__(self, lower_left_corner, width, height):
+ self.lower_left_corner = lower_left_corner # lower_left_corner is a Point object
+ self.width = width
+ self.height = height
+
+
+ p1 = Point(1, 2)
+ p2 = copy.copy(p1)
+
+ print(p1 is p2)
+ print(p1 == p2) # what if we don't have the __eq__ method
+
+ r1 = Rectangle(Point(0,0), 10, 4)
+ r2 = copy.copy(r1)
+ print(r1 is r2) # False
+ print(r1.lower_left_corner is r2.lower_left_corner) # True
+ print(r1.width is r2.width) # also True
+
+ r1.width = 20
+ print(r1.width is r2.width)
+ print(r2.width) # remain to be 10
+
+ r1 = Rectangle(Point(1,1), 8, 4)
+ r3 = copy.deepcopy(r1)
+ print(r1 is r3) # False
+ print(r1.lower_left_corner is r2.lower_left_corner) # False
+
+
+
+纯函数与修改函数
+````````````````````````````````````````````````````
+
+.. code:: python
+
+
+ class Time:
+ def __init__(self, h, m, s):
+ self.hour = h
+ self.minute = m
+ self.second = s
+
+
+ def increment_time_pure(t, h, m, s):
+ ''' Pure function '''
+ t2 = Time(0,0,0)
+ return t2
+
+ def increment_time_modifier(t, h, m, s):
+ ''' Modifier function '''
+ t2 = Time(0,0,0)
+ return t2
+
+ t = Time(6, 58, 55)
+ ti = increment_time_pure(t, 2, 30, 50)
+
+
+
+
web 应用程序
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+````````````
输入文本
``````````````````````````````````````````````````