python builtin keyword: property
2012-07-11 22:50
141 查看
该文章有误, 请google python descriptor
##################################################################################
property 这个东西用起来还是很简单得,见 http://docs.python.org/library/functions.html#property, 两种用法 :
和
这里我就奇怪了,一般我们写decorator都是用function,那这个property到底是class还是function?在ipython里help(property) 显示 property 定义是
想了下,property功能得实现应该如下:
真正property得实现应该比这复杂多了,但基本做法是这样。
还有一个疑问是怎么将property宿主得函数变成属性,代码如下:
其他a.x = 1, del a.x 这样得功能就是给MyProperty添加 __set__, __del__ 方法。
============================================================================
classproperty http://pypi.python.org/pypi/classproperty
这个代码太奇怪了,类继承于实例,看了很久都没看出所以然来,后来经人指点才知道了。先看个小例子
这样应该就可以明白 classpropertytype 为什么这么写, 为什么类可以继承于实例了。A.__call__ 很重要,没有它b就不会被创建,我猜想对象实例化得时候对父类应该是 有__new__属性就会去执行new, 不然就去执行__call__。
ps. 以上说的有误,pls google python descriptor -- 20121126.
##################################################################################
property 这个东西用起来还是很简单得,见 http://docs.python.org/library/functions.html#property, 两种用法 :
class C(object): def __init__(self): self._x = None def getx(self): return self._x def setx(self, value): self._x = value def delx(self): del self._x x = property(getx, setx, delx, "I'm the 'x' property.")
和
class Parrot(object): def __init__(self): self._voltage = 100000 @property def voltage(self): """Get the current voltage.""" return self._voltage
这里我就奇怪了,一般我们写decorator都是用function,那这个property到底是class还是function?在ipython里help(property) 显示 property 定义是
class property(object) | property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
想了下,property功能得实现应该如下:
class MyProperty(object): def __init__(self, fget): print 'my property init' self._fget = fget def __call__(self): print 'my property call' return self._fget() @MyProperty def foo(): print 'foo' print '===', foo foo()
真正property得实现应该比这复杂多了,但基本做法是这样。
还有一个疑问是怎么将property宿主得函数变成属性,代码如下:
class MyProperty(object): def __init__(self, func): print 'my property init' self._func = func def __get__(self, obj, cls=None): return self._func(obj) class A(object): def __init__(self): self._x = 10 @MyProperty def x(self): return self._x a = A() print a.x
其他a.x = 1, del a.x 这样得功能就是给MyProperty添加 __set__, __del__ 方法。
============================================================================
classproperty http://pypi.python.org/pypi/classproperty
这个代码太奇怪了,类继承于实例,看了很久都没看出所以然来,后来经人指点才知道了。先看个小例子
class A(object): def __init__(self, *args): print 'A init' print self, args super(A, self).__init__() def __call__(self): print 'A callable' return self a = A() print a() print '=============================' class B(a): def __init__(self): super(B, self).__init__() b = B() 输出是: A init <__main__.A object at 0xb72bc38c> () A callable <__main__.A object at 0xb72bc38c> ============================= A init <__main__.A object at 0xb72bc3ec> ('B', (<__main__.A object at 0xb72bc38c>,), {'__module__': '__main__', '__init__': <function __init__ at 0xb72adc6c>}) A callable
这样应该就可以明白 classpropertytype 为什么这么写, 为什么类可以继承于实例了。A.__call__ 很重要,没有它b就不会被创建,我猜想对象实例化得时候对父类应该是 有__new__属性就会去执行new, 不然就去执行__call__。
ps. 以上说的有误,pls google python descriptor -- 20121126.
相关文章推荐
- Python built-in 函数
- 深入理解Python中的__builtin__和__builtins__
- python ImportError: No module named builtins
- Python __builtins__模块拾穗
- Python -- Built in functions
- java.lang.NoSuchMethodError: java.lang.NoSuchMethodError: org.python.core.PyBuiltinFunctionSet.<init
- Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)
- Python built-in function - any(iterable)
- 深入解析Python中的__builtins__内建对象
- Python的__builtins__模块中的一些要点知识
- Python built-in function - ascii(object)
- Python built-in function - bin(x)
- What does the yield keyword do in Python?
- Property in Python
- Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
- 详细介绍 Python-__builtin__与__builtins__和builtins的区别与关系
- 深入理解Python中的__builtin__和__builtins__ 推荐
- Python-__builtin__与__builtins__的区别与关系
- python2,socket多进程的错误pickle.PicklingError: Can't pickle <built-in method recvfrom_into of _socket.soc
- Python中__builtin__模块和__builtins__模块的区别和联系