7-4 如何创建可管理的对象属性

Posted 石中玉smulngy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了7-4 如何创建可管理的对象属性相关的知识,希望对你有一定的参考价值。

>>> help(property)
Help on class property in module __builtin__:

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
 |  
 |  Methods defined here:
 |  
 |  __delete__(...)
 |      descr.__delete__(obj)
 |  
 |  __get__(...)
 |      descr.__get__(obj[, type]) -> value
 |  
 |  __getattribute__(...)
 |      x.__getattribute__(\'name\') <==> x.name
 |  
 |  __init__(...)
 |      x.__init__(...) initializes x; see help(type(x)) for signature
 |  
 |  __set__(...)
 |      descr.__set__(obj, value)
 |  
 |  deleter(...)
 |      Descriptor to change the deleter on a property.
 |  
 |  getter(...)
 |      Descriptor to change the getter on a property.
 |  
 |  setter(...)
 |      Descriptor to change the setter on a property.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  fdel
 |  
 |  fget
 |  
 |  fset
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T
help(property)

property()三个参数分别是访问方法,设置方法。

 

from math import pi

class Circle(object):
    def __init__(self,radius):
        self.radius = radius

    def getRadius(self):
        return self.radius
    def setRadius(self,value):
        if not isinstance(value,(int,long,float)):
            raise ValueError(\'wrong data type,please slecet int or long or float\')
        self.radius = value

    def getArea(self):
        return self.radius ** 2 * pi
    R = property(getRadius,setRadius)


c = Circle(3.2)

print (c.getArea())

print (c.R)                #以属性方式访问,实际上调用的是getRadius

c.R = 4                    #以属性方式访问,实际上调用的是setRadius

print(c.R)          
print(c.getArea())

 

输出结果:

32.1699087728

3.2

4

50.2654824574

 

以上是关于7-4 如何创建可管理的对象属性的主要内容,如果未能解决你的问题,请参考以下文章

python-类与对象 如何创建可管理的对象属性 (图文并茂)

python-类与对象 如何创建可管理的对象属性 (图文并茂)

python-类与对象 如何创建可管理的对象属性 (图文并茂)

创建可管理的对象属性

创建可管理的对象属性

如何创建静态可绑定属性描述对象