美文网首页
property属性

property属性

作者: FangHao | 来源:发表于2017-06-06 23:01 被阅读0次

    property函数的作用

    讲方法转换为只读

    重新设定一个属性设置和读取属性

    In [1]: class Money(object):
       ...:     def __init__(self):
       ...:         self.__money = 0
       ...:     @property
       ...:     def money(self):
       ...:         return self.__money
       ...:     @money.setter
       ...:     def money(self,value):
       ...:         if isinstance(value,int):
       ...:             self.__money = value
       ...:         else:
       ...:             print 'error:not integer'
       ...:
    
    In [2]: a = Money()
    
    In [3]: a.money
    Out[3]: 0
    
    In [4]: a._Money__money
    Out[4]: 0
    
    In [5]: a.money = 100
    
    In [6]: a.money
    Out[6]: 100
    
    In [7]: a._Money__money
    Out[7]: 100
    

    相关文章

      网友评论

          本文标题:property属性

          本文链接:https://www.haomeiwen.com/subject/fsnnfxtx.html