函数property的基本功能就是把类中的方法当作属性来访问,下面以一个有意思的例子介绍一下:
假如有一只猫,它忘了它喜欢吃什么,下面看看我们如何治好它吧
原代码: #运行环境 python2.7.10
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
classCat(object):
def__init__(self,food):
self.food=food
defeat(self):
returnself.food
defsay(self):
if'im_func'indir(self.eat):
print"I forgot what to eat,Mybe %s"%self.food
else:
print"Miao...,I want to eat fish!"
if__name__=="__main__":
tim=Cat('Stone')
tim.say()
|
运行后,这只猫说“I forgot what to eat,Mybe Stone”
看来是真有问题了
来个小改动吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
classCat(object):
def__init__(self,food):
self.food=food
@property
defeat(self):
returnself.food
defsay(self):
if'im_func'indir(self.eat):
print"I forgot what to eat,Mybe %s"%self.food
else:
print"Miao...,I want to eat fish!"
if__name__=="__main__":
tim=Cat('Stone')
tim.say()
|
“Miao...,I want to eat fish!”
看来情况出在@property这个装饰器上
假如我们把@property去掉
在后边加一行代码
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
|
#!/usr/bin/python
#-*-utf-8-*-
classCat(object):
def__init__(self,food):
self.food=food
@property
defeat(self):
returnself.food
defsay(self):
if'im_func'indir(self.eat):
print"I forgot what to eat,Mybe %s"%self.food
else:
print"Miao...,I want to eat fish!"
if__name__=="__main__":
tim=Cat('Stone')
tim.say()
try:
tim.eat="Is fish deliciout?"
printtim.eat
except:
print"^^,Fish is so nice"
<div>
</div>
|
输出:
I forgot what to eat,Mybe Stone
Is fish deliciout?
输出:
Miao...,I want to eat fish!
^^,Fish is so nice
总结一下:加上了@property修饰的方法就变成了数据属性了,不再是方法了,如下:
class Cat(object):
def __init__(self,food):
self.food = food
#@property
def eat(self):
return self.food
def say(self):
print dir(self.eat)
if 'im_func' in dir(self.eat):
print "i forgot what to eat,maybe %s"%self.food
else:
print "meow..., I want to eat fish!"
if __name__ =='__main__':
tim = Cat('stone')
tim.say()
try:
tim.eat = "Is fish delicious?"
print tim.eat
except:
print '^^,fish is so nice!'
运行结果是:
meow..., I want to eat fish!
^^,fish is so nice!
如果是以下代码:
class Cat(object):
def __init__(self,food):
self.food = food
#@property
def eat(self):
return self.food
def say(self):
print dir(self.eat)
if 'im_func' in dir(self.eat):
print "i forgot what to eat,maybe %s"%self.food
else:
print "meow..., I want to eat fish!"
if __name__ =='__main__':
tim = Cat('stone')
tim.say()
try:
tim.eat = "Is fish delicious?"
print tim.eat
except:
print '^^,fish is so nice!'
运行结果是:
i forgot what to eat,maybe stone
Is fish delicious?
如果是以下代码:
class Cat(object):
def __init__(self,food):
self.food = food
@property
def eat(self):
return self.food
def say(self):
print dir(self.eat)
if 'im_func' in dir(self.eat):
print "i forgot what to eat,maybe %s"%self.food
else:
print "meow..., I want to eat fish!"
if __name__ =='__main__':
tim = Cat('stone')
tim.say()
try:
tim.eat1 = "Is fish delicious?"
print tim.eat1
except:
print '^^,fish is so nice!'
运行结果是:
meow..., I want to eat fish!
Is fish delicious?
在这里虽然tim.eat已经不再是方法了,而是数据属性了,但是由于我们在主函数里面没有修改tim.eat 而是修改了tim.eat1,所以得到的结果是上面的结果,呵呵
最主要的就是经过property修饰后函数不再是函数,方法不再是方法,而是数据属性了,