python基础===装饰器@property

以下来自Python 3.6.0 Document:
class property(fget=None, fset=None, fdel=None, doc=None)

Return a property attribute.

fget is a function for getting an attribute value. fset is a function for setting an attribute value. fdel is a function for deleting an attribute value. And doc creates a docstring for the attribute.

A typical use is to define a managed attribute x:

class C:
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x

    def setx(self, value):
        self._x = value

    def delx(self):
        del self._x

    x = property(getx, setx, delx, "I‘m the ‘x‘ property.")

If c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.

If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget‘s docstring (if it exists). This makes it possible to create read-only properties easily using property() as a decorator:

class Parrot:
    def __init__(self):
        self._voltage = 100000

    @property
    def voltage(self):
        """Get the current voltage."""
        return self._voltage

The @property decorator turns the voltage() method into a “getter” for a read-only attribute with the same name, and it sets the docstring for voltage to “Get the current voltage.”

A property object has getter, setter, and deleter methods usable as decorators that create a copy of the property with the corresponding accessor function set to the decorated function. This is best explained with an example:

class C:
    def __init__(self):
        self._x = None

    @property
    def x(self):
        """I‘m the ‘x‘ property."""
        return self._x

    @x.setter
    def x(self, value):
        self._x = value

    @x.deleter
    def x(self):
        del self._x

This code is exactly equivalent to the first example. Be sure to give the additional functions the same name as the original property (x in this case.)

The returned property object also has the attributes fget, fset, and fdel corresponding to the constructor arguments.

Changed in version 3.5: The docstrings of property objects are now writeable.

先看一段代码:

class stu:
    def __init__(self,name):
        self.name = name

    def get_score(self):
        return self.score

    def set_score(self, score):
        if not isinstance(score, int):
            raise ValueError("score must be an integer~")
        if score>100 or score<0:
            raise ValueError("score must between 0~100")
        self.score = score

s = stu("Botoo")
#s.set_score(100)            #score must be an integer~
#s.set_score("dasda")    #score must between 0~100
s.set_score(98)
print(s.get_score())        #98

这种使用 get/set 方法来封装对一个属性的访问在许多面向对象编程的语言中都很常见。

但是写 s.get_score() 和 s.set_score() 没有直接写 s.score 来得直接。

因为Python支持高阶函数,可以用装饰器函数把 get/set 方法“装饰”成属性调用:

再比较:

class Student:
    def __init__(self,name):
        self.name = name

    @property
    def score(self):
        return self._score

    @score.setter
    def score(self,value):
        if not isinstance(value, int):
            raise ValueError(‘分数必须是整数才行呐‘)
        if value < 0 or value > 100:
            raise ValueError(‘分数必须0-100之间‘)
        self._score = value

S1 = Student("botoo")
S1.score = 50

print(S1.score)         #50
S1.score = 500         #ValueError: 分数必须0-100之间

第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品。

在子类中扩展一个property可能会引起很多不易察觉的问题, 因为一个property其实是 gettersetterdeleter 方法的集合,而不是单个方法。 因此,当你扩展一个property的时候,你需要先确定你是否要重新定义所有的方法还是说只修改其中某一个。

参考:

http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p08_extending_property_in_subclass.html

https://blog.csdn.net/u013205877/article/details/77804137

原文地址:https://www.cnblogs.com/botoo/p/8883944.html

时间: 2024-08-29 12:28:43

python基础===装饰器@property的相关文章

python基础—装饰器

python基础-装饰器 定义:一个函数,可以接受一个函数作为参数,对该函数进行一些包装,不改变函数的本身. def foo(): return 123 a=foo(); b=foo; print(a) print(b) 123 <function foo at 0x000001608E2B97B8> 解释:把函数foo赋值给a和b,a 赋值的是调用后的函数,变量的值就是返回值.b 赋值的是调用前的函数,所以b 就是那个赋值的函数.函数本身+(),就是调用. callable(a) False

python基础-装饰器和偏函数

一.装饰器 1.概念:装饰器是一个闭包(内层函数引用外层函数的非全局变量,这个内层函数就可称之为闭包),把一个函数当做参数返回一个替代版的函数,本质上就是一个返回函数的函数. 2.作用:在不修改源代码的基础上,扩展代码的使用. 3.理解装饰器 #不带参数的源函数 def fun1(): print("I love python") fun1() #若我想在原来的基础上,先输出一行的*号,我们可以重新写一个函数 def fun2(): print("***********&qu

python基础---装饰器

1,装饰器: 1)为什么要用装饰器:因为你开发的程序一旦上市,就要遵守源代码开放并且尽量不能修改源代码,函数的调用方式也尽量不要修改,新的需求来了,每一       款软件都是需要更新的,在不修改源代码,不修改函数调用方式,同时还要增加新的功能,怎么实现呢?所以有了装饰器,来满足我们的条件. 2)什么是装饰器:顾名思义,装饰就是修饰,器相当于函数 3)装饰器定义:其本质就是一个函数,功能是为其他函数添加新的一个功能. 2,举例: 1) 现在来建立一个最简单的函数,这组函数值打印"welcome

python学习之类和实例的属性;装饰器@property

无论是类还是实例,一切皆是对象. Python是强动态语言,和java在这点上有所不同. class Ab(): a = 666 # 定义类对象Ab,自带属性a,值为666 # 使用Ab.__dict__可以查看类Ab的属性 us1 = Ab() us2 = Ab() # 定义两个实例对象us1.us2,这两个实例自身并不具备任何属性 # 只有在__init__中定义了self.arg=xxx的情况下,实例默认会具备arg属性 动态语言中,属性自带操作方法:获取(读).设置(写):还可以定义删除

python 之用装饰器@property,把方法变成一个特性

# -*- coding: utf-8 -*- """ Created on Sun Nov 13 23:19:03 2016 @author: toby """ #知识点:用装饰器@property,把方法变成一个特性 class Province:     memo = 'One of China\'s 23 provinces' #静态字段          def __init__(self,name,capital,leadership

【转】详解Python的装饰器

原文链接:http://python.jobbole.com/86717/ Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def say_hello(): print "hello!" def say_goodbye(): print "hello!" # bug here if __name__ == '__main__':

详解Python的装饰器

Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def say_hello(): print "hello!" def say_goodbye(): print "hello!" # bug here if __name__ == '__main__': say_hello() say_goodbye() 但是在实际调用中,我们

说说Python的装饰器模式与面向切面编程

说说Python的装饰器模式与面向切面编程 今天来讨论一下装饰器.装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身无关的雷同代码并继续重用.概括的讲,装饰器的作用就是为已经存在的对象添加额外的功能. 1. 装饰器入门 1.1. 需求是怎么来的? 装饰器的定义很是抽象,我们来看一个小例子. //edit http://www.lai18.com //date 2

Python学习---装饰器的学习1210

装饰器的基础 学习前提: 作用域 + 函数的理解 + 闭包  [学习,理解] 代码编写原则: 对修改开放对扩展开放 装饰器本质上是一个函数,该函数用来处理其他函数,它可以让其他函数在不需要修改代码的前提下增加额外的功能,装饰器的返回值也是一个函数对象. 装饰器的应用:经常用于有切面需求的场景,比如:插入日志.性能测试.事务处理.缓存.权限校验等应用场景.装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量与函数功能本身无关的雷同代码并继续重用. 装饰器作用:装饰器感觉就像是内部函数的