zoukankan      html  css  js  c++  java
  • python面向对象高级:@property

    @property

    把方法『变成』了属性,广泛应用在类的定义中,可以让调用者写出简短的代码,同时保证对参数进行必要的检查,这样,程序运行时就减少了出错的可能性。

    最大的作用就是既能检查参数,又可以用类似属性这样简单的方式来访问类的变量!

    #在使用 @property 之前,让我们先来看一个简单的例子:
    
    class Exam(object):
        def __init__(self, score):
            self._score = score
    
        def get_score(self):
            return self._score
    
        def set_score(self, val):
            if val < 0:
                self._score = 0
            elif val > 100:
                self._score = 100
            else:
                self._score = val
    
    >>> e = Exam(60)
    >>> e.get_score()
    60
    >>> e.set_score(70)
    >>> e.get_score()
    70

    在上面,我们定义了一个 Exam 类,为了避免直接对 _score 属性操作,我们提供了 get_score 和 set_score 方法,这样起到了封装的作用,把一些不想对外公开的属性隐蔽起来,而只是提供方法给用户操作,在方法里面,我们可以检查参数的合理性等。

    这样做没什么问题,但是我们有更简单的方式来做这件事,Python 提供了 property 装饰器,被装饰的方法,我们可以将其『当作』属性来用,看下面的例子:

    class Exam(object):
        def __init__(self, score):
            self._score = score
    
        @property
        def score(self):
            return self._score
    
        @score.setter
        def score(self, val):
            if val < 0:
                self._score = 0
            elif val > 100:
                self._score = 100
            else:
                self._score = val
    
    >>> e = Exam(60)
    >>> e.score
    60
    >>> e.score = 90
    >>> e.score
    90
    >>> e.score = 200
    >>> e.score
    100

    在上面,我们给方法 score 加上了 @property,于是我们可以把 score 当成一个属性来用,此时,又会创建一个新的装饰器 score.setter,它可以把被装饰的方法变成属性来赋值。

    另外,我们也不一定要使用 score.setter 这个装饰器,这时 score 就变成一个只读属性了:

    class Exam(object):
        def __init__(self, score):
            self._score = score
    
        @property
        def score(self):
            return self._score
    
    >>> e = Exam(60)
    >>> e.score
    60
    >>> e.score = 200  # score 是只读属性,不能设置值
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-676-b0515304f6e0> in <module>()
    ----> 1 e.score = 200
    
    AttributeError: can't set attribute

    原文地址:使用@property,学习内容参考:廖雪峰文档用property快速重构代码

  • 相关阅读:
    泛型集合的变化
    c#中结构与类的区别
    C# where子句
    C# 泛型学习
    about osgeo中国
    about codeplex
    content in map
    a excellent website for javascrpt & dhtml:
    MapGuide open source开发系列教程四: 体系结构(转贴)
    about NetTopologySuite
  • 原文地址:https://www.cnblogs.com/kumata/p/9163172.html
Copyright © 2011-2022 走看看