-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpython_class.py
More file actions
144 lines (109 loc) · 3.28 KB
/
python_class.py
File metadata and controls
144 lines (109 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/local/bin/python
# -*- coding: UTF-8 -*-
class MyClass:
"""A simple example class"""
i = 12345
__update = "update" # private copy of original update() method
def __init__(self):
print("No no, __init__")
def f(self):
print("Hello World!")
print(MyClass.i)
try:
MyClass.f()
except:
print("Exception...")
x = MyClass()
x.f()
try:
print(x.__update)
except:
print("This is private")
# 数据属性 相当于 Smalltalk 中的“实例变量”或 C++ 中的“数据成员”。和局部变量一样,数据属性不需要声明,
x.counter = 1
while x.counter < 10:
x.counter = x.counter * 2
print(x.counter)
del x.counter
# class DerivedClassName(BaseClassName):
# class DerivedClassName(modname.BaseClassName):
# BaseClassName.methodname(self, arguments):
# 多继承:
#############################################
# 你能想到的搜索属性从父类继承的深度优先,左到右,而不是搜索两次在同一个类层次结构中,其中有一个重叠。
# 因此,如果在 DerivedClassName (示例中的派生类)中没有找到某个属性,就会搜索 Base1 ,然后(递归的)搜索其基类,
# 如果最终没有找到,就搜索 Base2 ,以此类推。
# class DerivedClassName(Base1, Base2, Base3):
# <statement-1>
# .
# .
# .
# <statement-N>
# 有时类似于 Pascal 中“记录(record)”或C中“结构(struct)”的数据类型很有用,
# 它将一组已命名的数据项绑定在一起。一个空的类定义可以很好的实现这它:
class Employee:
pass
john = Employee() # Create an empty employee record
# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000
class B(Exception):
pass
class C(B):
pass
class D(C):
pass
for cls in [B, C, D]:
try:
raise cls()
except D:
print("D")
except C:
print("C")
except B:
print("B")
# Iterator 迭代器
s = 'abcd'
it = iter(s)
print(it)
print(next(it))
print(it.next())
print(it.next())
print(it.next())
# print(it.next())
class Reverse:
"""Iterator for looping over a sequence backwards."""
def __init__(self, data):
self.data = data
self.index = len(data)
def __iter__(self):
return self
def __next__(self):
if self.index == 0:
# raise StopInteration
pass
self.index = self.index - 1
return self.data[self.data]
rev = Reverse('spam')
# print(iter(rev))
# for char in rev:
# print(char)
# generator 生成器
# Generator 是创建迭代器的简单而强大的工具。它们写起来就像是正规的函数,需要返回数据的时候使用 yield 语句。
# 每次 next() 被调用时,生成器回复它脱离的位置(它记忆语句最后一次执行的位置和所有的数据值)。
def reverse(data):
for index in range(len(data)-1,-1,-1):
yield data[index]
for char in reverse("golfball"):
print(char)
print(sum(i*i for i in range(10))) # sum of squares)
xvec = [10, 20, 30]
yvec = [7, 5, 3]
print(sum(x*y for x,y in zip(xvec, yvec))) # dot product)
# from math import pi, sin
# sine_table = {x: sin(x*pi/180) for x in range(0, 91)}
# unique_words = set(word for line in page for word in line.split())
# valedictorian = max((student.gpa, student.name) for student in graduates)
data = 'golf'
print(list(data[i] for i in range(len(data)-1, -1, -1)))