什么是 属性 对象?
What is a property object?
我不是 python 的新手,但我有一个非常基本的问题。
我在玩弄python,发现有类型属性
>>> property
<type 'property'>
但是我只听说过函数上下文中的属性。
>>> a = property()
<property object at 0x0246C090>
但是 属性 个对象呢?他们有什么用? 属性 方法不是很直观或暗示
>>> 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']
感谢关注!
一个属性是一个包含getter和setter方法的属性对象。
class MyClass:
def __init__(self,*costs):
self.costs = costs
def item_cost(self):
return sum(self.costs)
现在你可以做
MyClass(1,2,3,4).item_cost() #prints 10
但我们可以把它变成 属性
class MyClass:
def __init__(self,*costs):
self.costs = costs
@property
def item_cost(self):
return sum(self.costs)
现在我们可以将它作为一个简单的变量来访问
MyClass(1,2,3,4).item_cost
您还可以为
的值创建一个 setter
...
@item_cost.setter
def set_item_cost(self,value):
pass #do something with value
...
MyClass(1,2,3,4).item_cost = "yellow"
总的来说,我发现它们有点反模式......但有些人喜欢 em
(旁注,您也可以将其设为 属性,将其用作常规函数而不是装饰器 MyClass.item_cost_prop = property(MyClass.item_cost)
)
property
对象就是您实际认为的 属性。考虑这个例子:
class Foo(object):
def __init__(self):
self._bar = 0
@property
def bar(self):
return self._bar + 5
Foo.bar
是一个 属性 对象,它有一个 __get__
方法。当你写类似
的东西时
x = Foo()
print(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 = 0
bar = property(lambda self: self._bar + 5)
更明确地表明您正在创建一个 property
对象,其中给定的 lambda
表达式作为 属性 的 getter,并将该对象绑定到class 属性 bar
.
property
class(以及实例方法、class方法和静态方法)是Python的通用描述符协议的具体应用,它使用 __get__
、__set__
、and/or __del__
方法定义 class 属性的行为。
我不是 python 的新手,但我有一个非常基本的问题。
我在玩弄python,发现有类型属性
>>> property
<type 'property'>
但是我只听说过函数上下文中的属性。
>>> a = property()
<property object at 0x0246C090>
但是 属性 个对象呢?他们有什么用? 属性 方法不是很直观或暗示
>>> 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']
感谢关注!
一个属性是一个包含getter和setter方法的属性对象。
class MyClass:
def __init__(self,*costs):
self.costs = costs
def item_cost(self):
return sum(self.costs)
现在你可以做
MyClass(1,2,3,4).item_cost() #prints 10
但我们可以把它变成 属性
class MyClass:
def __init__(self,*costs):
self.costs = costs
@property
def item_cost(self):
return sum(self.costs)
现在我们可以将它作为一个简单的变量来访问
MyClass(1,2,3,4).item_cost
您还可以为
的值创建一个 setter ...
@item_cost.setter
def set_item_cost(self,value):
pass #do something with value
...
MyClass(1,2,3,4).item_cost = "yellow"
总的来说,我发现它们有点反模式......但有些人喜欢 em
(旁注,您也可以将其设为 属性,将其用作常规函数而不是装饰器 MyClass.item_cost_prop = property(MyClass.item_cost)
)
property
对象就是您实际认为的 属性。考虑这个例子:
class Foo(object):
def __init__(self):
self._bar = 0
@property
def bar(self):
return self._bar + 5
Foo.bar
是一个 属性 对象,它有一个 __get__
方法。当你写类似
x = Foo()
print(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 = 0
bar = property(lambda self: self._bar + 5)
更明确地表明您正在创建一个 property
对象,其中给定的 lambda
表达式作为 属性 的 getter,并将该对象绑定到class 属性 bar
.
property
class(以及实例方法、class方法和静态方法)是Python的通用描述符协议的具体应用,它使用 __get__
、__set__
、and/or __del__
方法定义 class 属性的行为。