什么是属性对象? [英] What is a property object?
问题描述
我对 Python 并不陌生,但我在这里有一个非常基本的问题.
我在玩python,发现有type属性
<预><代码>>>>财产<输入属性">但我只听说过函数上下文中的属性.
<预><代码>>>>a = 属性()<0x0246C090处的属性对象>但是属性对象呢?他们有什么用?属性方法不是很直观或暗示性
<预><代码>>>>目录(一)['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce'__ex__'__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']感谢您的关注!
property
对象实际上是您认为的属性.考虑这个例子:
class Foo(object):def __init__(self):self._bar = 0@财产定义栏(自我):返回 self._bar + 5
Foo.bar
是一个具有 __get__
方法的属性对象.当你写一些像
x = Foo()打印(x.bar)
查找 x.bar
发现 type(x).bar
有一个 __get__
方法,因此属性查找变得等效
type(x).bar.__get__(x, type(x))
产生值 x._bar + 5
.
使用 property
作为装饰器在某种程度上掩盖了 bar
是一个 property
对象的事实.一个等效的定义是
class Foo(object):def __init__(self):self._bar = 0bar = 属性(lambda 自我:self._bar + 5)
它更明确地表明您正在创建一个 property
对象,使用给定的 lambda
表达式作为该属性的 getter,并将该对象绑定到类属性 bar
.
property
类(以及实例方法、类方法和静态方法)是 Python 通用描述符协议的具体应用,它通过 __get__定义了类属性的行为code>、
__set__
和/或 __del__
方法.
I am not new to python, but I have a pretty basic question here.
I was playing around with python and found that there is the type property
>>> property
<type 'property'>
But I have only heard of properties in the function context.
>>> a = property()
<property object at 0x0246C090>
But what about property objects? What are they use? Property methods are not very intuitive or suggestive
>>> dir(a)
['__class__', '__delattr__', '__delete__', '__doc__', '__format__', '__get__', '__getattribute__', '__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__set__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'deleter', 'fdel', 'fget', 'fset', 'getter', 'setter']
Thank you for the attention!
The property
object is what you are actually thinking of as a property. Consider this example:
class Foo(object):
def __init__(self):
self._bar = 0
@property
def bar(self):
return self._bar + 5
Foo.bar
is a property object which has a __get__
method. When you write something like
x = Foo()
print(x.bar)
the lookup for x.bar
finds that type(x).bar
has a __get__
method, and so the attribute lookup becomes equivalent to
type(x).bar.__get__(x, type(x))
which produces the value x._bar + 5
.
The use of property
as a decorator somewhat obscures the fact that bar
is a property
object. An equivalent defintion is
class Foo(object):
def __init__(self):
self._bar = 0
bar = property(lambda self: self._bar + 5)
which shows more explicitly that you are creating a property
object with the given lambda
expression as the getter for that property, and binding the object to the class attribute bar
.
The property
class (along with instance methods, class methods, and static methods) is a specific application of Python's general descriptor protocol, which defines the behavior of class attributes with __get__
, __set__
, and/or __del__
methods.
这篇关于什么是属性对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!