Skip to content

Commit 2ac43de

Browse files
committed
add magic methods
1 parent a435e63 commit 2ac43de

File tree

6 files changed

+118
-0
lines changed

6 files changed

+118
-0
lines changed

py3/oop_advance/special_call.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Student(object):
5+
def __init__(self, name):
6+
self.name = name
7+
8+
def __call__(self):
9+
print('My name is %s.' % self.name)
10+
11+
s = Student('Michael')
12+
s()

py3/oop_advance/special_getattr.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Student(object):
5+
6+
def __init__(self):
7+
self.name = 'Michael'
8+
9+
def __getattr__(self, attr):
10+
if attr=='score':
11+
return 99
12+
if attr=='age':
13+
return lambda: 25
14+
raise AttributeError('\'Student\' object has no attribute \'%s\'' % attr)
15+
16+
s = Student()
17+
print(s.name)
18+
print(s.score)
19+
print(s.age())
20+
# AttributeError: 'Student' object has no attribute 'grade'
21+
print(s.grade)

py3/oop_advance/special_getitem.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Fib(object):
5+
6+
def __getitem__(self, n):
7+
if isinstance(n, int):
8+
a, b = 1, 1
9+
for x in range(n):
10+
a, b = b, a + b
11+
return a
12+
if isinstance(n, slice):
13+
start = n.start
14+
stop = n.stop
15+
if start is None:
16+
start = 0
17+
a, b = 1, 1
18+
L = []
19+
for x in range(stop):
20+
if x >= start:
21+
L.append(a)
22+
a, b = b, a + b
23+
return L
24+
25+
f = Fib()
26+
print(f[0])
27+
print(f[5])
28+
print(f[100])
29+
print(f[0:5])
30+
print(f[:10])

py3/oop_advance/special_iter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Fib(object):
5+
6+
def __init__(self):
7+
self.a, self.b = 0, 1 # 初始化两个计数器a,b
8+
9+
def __iter__(self):
10+
return self # 实例本身就是迭代对象,故返回自己
11+
12+
def __next__(self):
13+
self.a, self.b = self.b, self.a + self.b # 计算下一个值
14+
if self.a > 100000: # 退出循环的条件
15+
raise StopIteration();
16+
return self.a # 返回下一个值
17+
18+
for n in Fib():
19+
print(n)

py3/oop_advance/special_str.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Student(object):
5+
6+
def __init__(self, name):
7+
self.name = name
8+
9+
def __str__(self):
10+
return 'Student object (name: %s)' % self.name
11+
12+
__repr__ = __str__
13+
14+
print(Student('Michael'))

py3/oop_advance/use_property.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
class Student(object):
5+
6+
@property
7+
def score(self):
8+
return self._score
9+
10+
@score.setter
11+
def score(self, value):
12+
if not isinstance(value, int):
13+
raise ValueError('score must be an integer!')
14+
if value < 0 or value > 100:
15+
raise ValueError('score must between 0 ~ 100!')
16+
self._score = value
17+
18+
s = Student()
19+
s.score = 60
20+
print('s.score =', s.score)
21+
# ValueError: score must between 0 ~ 100!
22+
s.score = 9999

0 commit comments

Comments
 (0)