zoukankan      html  css  js  c++  java
  • property:get、set

    property属性:自动调用get、set方法
    每次调用隐藏的数据,都用get和set方法写的字符太长,就用property解决。
    方法一:使用property()函数升级get、set方法
     
    源代码:
    class Money(object):
        def __init__(self):
            self.__money = 0
     
        def get_money(self):
            return self.__money
     
        def set_money(self, value):
            if isinstance(value, int):
                self.__money = value
            else:
                print("error:不是整型的数字")
        # 定义一个属性money,通过这个属性自动识别,并调用适用的方法。
        money = property(get_money, set_money)
     
    # 自动识别是get取值,还是set赋值
    m1 = Money()
    print(m1.money)   # 自动识别,并调用get_money方法
    m1.money = 100    # 自动识别,并调用set_money方法
    print(m1.money)
     
    方法二:property属性-装饰器
     
    源代码:
    class Money(object):
        def __init__(self):
            self.__money = 0
     
        @property      # 相当于get方法,必须写在set方法前面
        def money(self):
            return self.__money
     
        @money.setter  # 相当于set方法,必须写在get方法后面
        def money(self, value):
            if isinstance(value, int):
                self.__money = value
            else:
                print("error:不是整型的数字")
     
    # 自动识别是get取值,还是set赋值
    m1 = Money()
    print(m1.money)   # 自动识别,并调用money的get方法
    m1.money = 100    # 自动识别,并调用money的set方法
    print(m1.money)
     
    方法3:
        def _get_url(self):
            return self._url
    
        def _set_url(self, url):
            if isinstance(url, str):
                self._url = url
            else:
                raise TypeError('%s url must be str, got %s:' % (type(self).__name__,
                    type(url).__name__))
    
        url = property(_get_url, obsolete_setter(_set_url, 'url'))
    

      

        def _get_body(self):
            return self._body
    
        def _set_body(self, body):
            if body is None:
                self._body = b''
            elif not isinstance(body, bytes):
                raise TypeError(
                    "Response body must be bytes. "
                    "If you want to pass unicode body use TextResponse "
                    "or HtmlResponse.")
            else:
                self._body = body
    
        body = property(_get_body, obsolete_setter(_set_body, 'body'))
    

      

    方法4:只有get方法

        @property
        def meta(self):
            try:
                return self.request.meta
            except AttributeError:
                raise AttributeError(
                    "Response.meta not available, this response "
                    "is not tied to any request"
                )
    

      

  • 相关阅读:
    剑指Offer-30.连续子数组的最大和(C++/Java)
    剑指Offer-29.最小的K个数(C++/Java)
    UVA 1616 Caravan Robbers 商队抢劫者(二分)
    UVA 10570 Meeting with Aliens 外星人聚会
    UVA 11093 Just Finish it up 环形跑道 (贪心)
    UVA 12673 Erratic Expansion 奇怪的气球膨胀 (递推)
    UVA 10954 Add All 全部相加 (Huffman编码)
    UVA 714 Copying Books 抄书 (二分)
    UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
    codeforecs Gym 100286B Blind Walk
  • 原文地址:https://www.cnblogs.com/andy9468/p/8299438.html
Copyright © 2011-2022 走看看