首页 > 编程语言 > 详细

python property方法的使用

时间:2016-11-01 21:47:27      阅读:311      评论:0      收藏:0      [点我收藏+]

property的作用:
实例:
class C:
    def __init__(self, size = 10):
        self.size = size

    def getSize(self):
        return self.size

    def setSize(self, value):
        self.size = value

    def delSize(self):
        del self.size

    x = property(getSize, setSize, delSize)

c = C()
print(c.getSize())
print(c.x)
c.x = 18
print(c.x)
print(c.getSize())

输出:
10
10
18
18
如果程序复杂了,以后,把getSize, setSize, delSize,修改为getxSize, setxSize, delxSize。这样后面调用到的地方都得修改,
使用property后,只要修改类方法名及property里的方法名就可以了。


注:参考资料http://bbs.fishc.com/thread-51106-1-1.html

python property方法的使用

原文:http://lynnpaul.blog.51cto.com/6803462/1868119

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!