Skip to content

Commit eb1fcec

Browse files
author
kangxiaoyu
committed
--call--实例本身是可以调用的
1 parent 2e375fc commit eb1fcec

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

2017-04-20/tuple_abc/sliceFib.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
__author__ = '康晓宇'
4+
5+
class Fib(object):
6+
def __getitem__(self,item):
7+
if isinstance(item,int):
8+
a,b=1,1
9+
for x in range(item):
10+
a,b = b, a+b
11+
return n
12+
13+
if isinstance(item,slice):
14+
start = item.start
15+
stop = item.stop
16+
a,b = 1,1
17+
18+
L = []
19+
20+
for x in range(stop):
21+
if x >= start:
22+
L.append(a)
23+
a,b = b, a+b
24+
return L
25+
f = Fib()
26+
27+
#print f[5:10]
28+
29+
class Student(object):
30+
def __init__(self):
31+
self.name = "Mirs"
32+
33+
def __getattr__(self,item):
34+
if item == 'score':
35+
return 99
36+
if item == 'age':
37+
return lambda:25
38+
39+
def __call__(self):
40+
print("My name is %s..."%self.name)
41+
ss = Student()
42+
print ss.score
43+
print ss.age()
44+
45+
#---------------__call__------------------
46+
#instance,method, 可不可以, 类似于 instance()本身上调用呢, 在Python中, 是肯定的
47+
48+
#只要定义一个__call__() 就可以多实例进行调用
49+
50+
ss()

2017-04-20/tuple_abc/tuple1.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/env python
2+
# -*- coding:utf-8 -*-
3+
4+
classmates=(u'涂金龙',u'康晓宇',u'朱树娜')
5+
6+
print classmates
7+
print classmates[0]
8+
9+
t=(1,2)
10+
print t
11+
12+
t=()
13+
print t
14+
15+
t=(1,)
16+
print t
17+
18+
t=('a','b',['A','B'])
19+
print t
20+
t[2][0]='C'
21+
22+
print t
23+

0 commit comments

Comments
 (0)