python property内建函数的介绍
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python property内建函数的介绍,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3155字,纯文字阅读大概需要5分钟。
内容图文
![python property内建函数的介绍](/upload/InfoBanner/zyjiaocheng/1177/9370682f914a499180df49fa2168856f.jpg)
函数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修饰后函数不再是函数,方法不再是方法,而是数据属性了,
原文:http://my.oschina.net/yusi/blog/525745
内容总结
以上是互联网集市为您收集整理的python property内建函数的介绍全部内容,希望文章能够帮你解决python property内建函数的介绍所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。