File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ()
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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 ])
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff line change 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' ))
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments