python property 函数用法
2013-09-03 20:43
211 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
class C(object):
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
def __init__(self): self._x = None
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
def getx(self): return self._x
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
def setx(self, value): self._x = value
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
def delx(self): del self._x
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
x = property(getx, setx, delx, "I'm the 'x' property.")
如果要使用property函数,首先定义class的时候必须是object的子类。通过property的定义,当获取成员x的值时,就会调用getx函数,当给成员x赋值时,就会调用setx函数,当删除x时,就会调用delx函数。使用属性的好处就是因为在调用函数,可以做一些检查。如果没有严格的要求,直接使用实例属性可能更方便。
相关文章推荐
- Python中property函数用法实例分析
- python里面的join函数用法及简单示例
- python 中 print 函数用法总结
- Python内置函数Type()函数一个有趣的用法
- Python range() 函数用法
- python中的函数用法入门教程
- Python3.2 --- Print函数用法
- Python map和reduce函数用法示例
- Python中内建函数的简单用法说明
- Python 3 iter函数用法简述
- python re 模块 findall 函数用法简述
- 详细记录python的range()函数用法
- Python中的join()函数的用法
- python中map()函数的用法讲解
- python之函数用法divmod
- python之函数用法iter()
- python中__str__()函数的用法
- python之函数用法islower()
- Python函数参数中*与**的用法和区别
- python进阶用法2 【从帮助函数看python内存申请机制】