• python中的property属性


    python中的property属性

    一、总结

    一句话总结:

    @property装饰器就是负责把一个方法变成属性调用的。@property定义get方法,@score.setter定义score属性的set方法
    class Student(object):
        def __init__(self, score=0):
            self._score = score
        
        @property    
        def score(self):
            print("getting score")
            return self._score
        
        @score.setter
        def score(self, value):
            print("setting score")
            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
            
    s = Student(60)
    s.score
    print("=====================")
    s.score = 88
    s.score

    二、python中的property属性

    转自或参考:python中的property属性 - 小飞的学习笔记 - 博客园
    https://www.cnblogs.com/yunxiaofei/p/11216883.html

    1. 基本认识

    property属性可以用来给属性添加约束,比如温度属性,我们不允许低于-273度;成绩属性,我们不允许0分以下等等。而且使用property属性,将来修改约束条件的时候也很方便,可以在代码的调用方式不变的情况下改变结果。

    python中使用property属性有两种方法。使用@property装饰器和使用property()函数。

    我们通过廖雪峰官方网站的实例来对此加深认识。

    2. @property装饰器

    @property装饰器就是负责把一个方法变成属性调用的。如下实例就可以通过s.score来获得成绩,并且对score赋值之前做出了数据检查。

    class Student(object):
        def __init__(self, score=0):
            self._score = score
        
        @property    
        def score(self):
            print("getting score")
            return self._score
        
        @score.setter
        def score(self, value):
            print("setting score")
            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
            
    s = Student(60)
    s.score
    print("=====================")
    s.score = 88
    s.score

    3. property()函数

    python中关于property()函数的介绍如下,在jupyter notebook中输入property??,即可查看用法:

    从帮助中可以看出,property()函数可以接收4个参数,第一个参数对应获取,第二个参数对应设置,第三个参数对应删除,第四个参数对应注释,写法如下:

    class Student(object):
        def __init__(self, score=0):
            self._score = score
           
        def get_score(self):
            print("getting score")
            return self._score
        
        def set_score(self, value):
            print("setting score")
            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
            
        def del_score(self):
            print("delete score")
            del self._score
    
        score = property(get_score, set_score, del_score)
    
    s = Student(60)
    print(s.score)
    print("=====================")
    s.score = 88
    print(s.score)
    print("=====================")
    del s.score

    参考链接:

    [1] https://www.liaoxuefeng.com/wiki/1016959663602400/1017502538658208

    [2] https://www.programiz.com/python-programming/property

    [3] https://www.geeksforgeeks.org/python-property-function/

     
  • 相关阅读:
    ylb: SQL表的高级查询-子查询
    ylb:SQL 系统函数
    ylb:SQL 常用函数
    ylb:exists(存在)的应用实例
    ylb:子查询(嵌套子查询)和子查询(相关子查询)
    ylb:多表的连接与练习(第三方关联表的应用)
    ylb:表的结构的修改和基本约束
    ylb:SQL Server中的escape(逃逸)
    ylb:SQL Server中的时间函数
    ylb:创建数据库、表,对表的增查改删语句
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/13156296.html
Copyright © 2020-2023  润新知