zoukankan      html  css  js  c++  java
  • @property

    Python内置的@property装饰器就是负责把一个方法变成属性调用的:

    class Screen(object):
         def isint(self,px):
            if not isinstance(px,int):
                 raise ValueError('px must be an int')
            if px<0 :
                 raise ValueError('px must be not a negative number')
       
         @property
         def width(self):
             return self._width
        
         @width.setter
         def width(self,value):
             self.isint(value)
             self._width=value
            
         @property
         def height(self):
             return self._height
        
         @height.setter
         def height(self,value):
             self.isint(value)
             self._height=value
            
         @property
         def resolution(self):
             return self._width * self._height

    @property给一个Screen对象加上widthheight属性,以及一个只读属性resolution

    >>> s=Screen()
    >>> s.width=666
    >>> s.height=888
    >>> print(s.resolution)
    591408
    >>> print('s.width * s.height =%d? ' % s.resolution)
    s.width * s.height =591408?
    >>> print('s.width * s.height =%d  ' % s.resolution)
    s.width * s.height =591408 
    >>>

  • 相关阅读:
    正则表达式
    小弟新从csdn搬迁到博客园,欢迎大家关注
    做完牛腩新闻发布系统之后的收获(牛腩总结)
    ValidateRequest="false" 无效
    sql server小技巧-自动添加时间与主键自增长
    css初接触
    Spark的Rpct模块的学习
    插入排序
    选择排序
    冒泡排序
  • 原文地址:https://www.cnblogs.com/bang325/p/7183770.html
Copyright © 2011-2022 走看看