zoukankan      html  css  js  c++  java
  • Python使用@property装饰类方法

    Python版本:3.5.2

    假如我们有一个Student类,并在其中定义了一个score属性,但是score属性会被显露出去,没办法检查参数,导致成绩可以随意更改:

    stu = Student()
    stu.score = 9999
    

    这显然是不合逻辑的,为了限制score的范围,可以通过一个set_score()方法来设置成绩,并通过一个get_score()方法来获取成绩,这样的话,就可以实现参数的检查:

    class Student(object):
    
        def get_score(self):
             return self._score
    
        def set_score(self, value):
            if not isinstance(value, int):
                raise ValueError('score must be an integer!')
            if value < 0 or value > 100:
                raise ValueError('score must between 0 ~ 100!')
            self._score = value
    

    调用及结果如下:

    调用:
    
    stu = Student()
    stu.set_score(99)
    print(stu.get_score())
    stu.set_score(9999)
    
    结果:
    
    99
    
    Traceback (most recent call last):
        stu.set_score(9999)
        raise ValueError('score must between 0 ~ 100!')
    ValueError: score must between 0 ~ 100!
    

    但是,上面的调用方法又略显复杂,没有直接用属性那么直接简单。

    这个问题是可以得到解决的,装饰器可以给函数动态的加上功能,那么对于类方法,一样可以起作用,可以使用@property装饰器将类方法变成属性来调用:

    class Student(object):
    
        @property
        def score(self):
            return self._score
    
        @score.setter
        def score(self, value):
            if not isinstance(value, int):
                raise ValueError('score must be an integer!')
            if value < 0 or value > 100:
                raise ValueError('score must between 0 ~ 100!')
            self._score = value
    

    把一个get方法变成属性,只需要加上@property就可以,此时,@property本身又创建了另一个装饰器@score.setter,负责把set方法也变成属性并可以赋值,之后我们就可以直接对score进行属性的操作并带有参数的检查:

    调用:
    
    stu = Student()
    stu.score = 99
    print(stu.score)
    stu.score = 9999
    
    结果:
    
    99
    
    Traceback (most recent call last):
        stu.score = 9999
        raise ValueError('score must between 0 ~ 100!')
    ValueError: score must between 0 ~ 100!
    

    有了这个@property,我们就可以无顾虑的操作类的属性。

    如果我们我们在类中只定义了get方法(并用@property装饰),而没有定义set方法,那么这个属性就会变成只读属性。

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

  • 相关阅读:
    Docker
    java 8 特性
    RISC-V: custom instruction and its simulation(转)
    如何在GCC中添加一条RISC-V指令(转)
    The GNU MCU Eclipse RISC-V Embedded GCC build
    Adding custom instruction to RISCV ISA and running it on gem5 and spike(转)
    How to add a custom instruction to the RISC-V GCC tools?(转)
    Adding and using a new instruction
    vuex2
    jq 显示/隐藏 某个模块
  • 原文地址:https://www.cnblogs.com/GyForever1004/p/8414244.html
Copyright © 2011-2022 走看看