python 的property函数
来源:互联网 发布:知柏地黄丸治疗性早熟 编辑:程序博客网 时间:2024/05/21 03:29
class C(object):def __init__(self):self._x = Nonedef getx(self):print ("get x")return self._xdef setx(self, value):print ("set x")self._x = valuedef delx(self):print ("del x")del self._xx = property(getx, setx, delx, "I'm the 'x' property.")
如果要使用property函数,首先定义class的时候必须是object的子类。通过property的定义,当获取成员x的值时,就会调用getx函数,当给成员x赋值时,就会调用setx函数,当删除x时,就会调用delx函数。使用属性的好处就是因为在调用函数,可以做一些检查。如果没有严格的要求,直接使用实例属性可能更方便。
>>> t=C()
>>> t.x
get x
>>> print (t.x)
get x
None
>>> t.x='en'
set x
>>> print (t.x)
get x
en
>>> del t.x
del x
>>> print (t.x)
get x
- python 的property函数
- python中property函数的理解
- python property函数
- python property 函数用法
- python中的property函数
- python property函数
- python中的property函数
- Python属性函数property
- Python property() 函数
- Python中的@property和property函数
- java的setter、getter 以及python的@property函数
- Learning Python : 2 . property函数
- python 的 @property
- Python @property 的说明
- python的@property
- python property的用法
- 【python】@property 的使用
- python property的使用
- 如何使用泛型达到代码重用的目的
- iOS如何修改字体间距
- TCP扫描器的简单实现
- 总线乔芯片
- An introduction of Messagepack
- python 的property函数
- 不放弃
- 二维数组的列排序
- setuid函数的学习笔记 转自:http://blog.chinaunix.net/space.php?uid=1877180&do=blog&cuid=1132108
- 对一道微软面试题的思考
- Google App Engine: Another transaction by user xxx is already in progress for app yyy ...
- [JavaScript] 慎用window.event.cancelBubble
- Oracle 层次查询-学习笔记
- [架构]F1Engine v0.1 alpha使用说明