python3中的property有一个很有意思的功能,它能将类中的方法像类属性一样调用!
class property(fget=None, fset=None, fdel=None, doc=None)
我们先来简单了解一下这个property类,下面看一下官网给出的例子:
class C:
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.") # 这里的x相当于类属性
c = C() # 生成一个对象
c.x = 10 # 设置self._x=10,实际调用的就是类中setx方法
c.x # 获取self._x的值,实际调用的就是类中getx方法
del c.x # 删除self._x的值,实际调用的就是类中delx方法
是不是感觉很有意思,很不可思议!property中fget是一个函数,它获取属性值;fset是一个函数,它设置一个属性值;fdel是一个函数,它删除一个属性值;doc为该属性创建一个docstring。你只要在使用时在对应的形参位置放上你写的对应函数,就可以轻松使用了。
我们还可以将property作为装饰器来使用,还是使用官网的例子:
class C:
def __init__(self):
self._x = None
@property
def x(self):
"""I'm the 'x' property."""
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
property对象有getter、setter、deleter三个方法,getter获取属性值,setter设置属性值,deleter设置属性值,这个例子的效果跟上一个例子的效果完全相同!我们看到里面的方法名是一模一样的,但是达到的效果却是不同的。第一个x方法是获取属性值,第二个x方法是设置属性值,第三个x方法是删除属性值。
你看到这里是不是以为这一切都是property帮你做到的,错,错,错!其实property只做了一件事件,它将你的方法能像类属性一样使用,至于里面的查、删、改,其实都是你自己写的函数实现的!fget、fset、fdel、setter、deleter这些仅仅只是名字而且,方便你识别,其他什么作用都没有!
我们来看一下property的源代码:
class property(object):
"""
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute. Typical use is to define a managed attribute x:
class C(object):
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.")
Decorators make defining new properties or modifying existing ones easy:
class C(object):
@property
def x(self):
"I am the 'x' property."
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
"""
def deleter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the deleter on a property. """
pass
def getter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the getter on a property. """
pass
def setter(self, *args, **kwargs): # real signature unknown
""" Descriptor to change the setter on a property. """
pass
def __delete__(self, *args, **kwargs): # real signature unknown
""" Delete an attribute of instance. """
pass
def __getattribute__(self, *args, **kwargs): # real signature unknown
""" Return getattr(self, name). """
pass
def __get__(self, *args, **kwargs): # real signature unknown
""" Return an attribute of instance, which is of type owner. """
pass
def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of property.__init__
"""
property(fget=None, fset=None, fdel=None, doc=None) -> property attribute
fget is a function to be used for getting an attribute value, and likewise
fset is a function for setting, and fdel a function for del'ing, an
attribute. Typical use is to define a managed attribute x:
class C(object):
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.")
Decorators make defining new properties or modifying existing ones easy:
class C(object):
@property
def x(self):
"I am the 'x' property."
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
# (copied from class doc)
"""
pass
@staticmethod # known case of __new__
def __new__(*args, **kwargs): # real signature unknown
""" Create and return a new object. See help(type) for accurate signature. """
pass
def __set__(self, *args, **kwargs): # real signature unknown
""" Set an attribute of instance to value. """
pass
fdel = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
fget = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
fset = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
__isabstractmethod__ = property(lambda self: object(), lambda self, v: None, lambda self: None) # default
看到上面的源代码恍然大悟没,fdel、fget、fset都只是执行你函数里面的代码而已!所以我们就记住一句话就够了:“property能让你的方法像类属性一样使用”。