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 anddel 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 thevoltage()
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
, anddeleter
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
, andfdel
corresponding to the constructor arguments.Changed in version 3.5: The docstrings of property objects are now writeable.
- class C:
先看一段代码:
- 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()) #
这种使用 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) #
- S1.score = 500 #ValueError: 分数必须0-100之间
第一个score(self)是get方法,用@property装饰,第二个score(self, score)是set方法,用@score.setter装饰,@score.setter是前一个@property装饰后的副产品。
在子类中扩展一个property可能会引起很多不易察觉的问题, 因为一个property其实是 getter
、setter
和 deleter
方法的集合,而不是单个方法。 因此,当你扩展一个property的时候,你需要先确定你是否要重新定义所有的方法还是说只修改其中某一个。
- 只有@property表示只读。
- 同时有@property和@x.setter表示可读可写。
- 同时有@property和@x.setter和@x.deleter表示可读可写可删除。
参考:
http://python3-cookbook.readthedocs.io/zh_CN/latest/c08/p08_extending_property_in_subclass.html
https://blog.csdn.net/u013205877/article/details/77804137
https://blog.csdn.net/sxingming/article/details/52916249
python基础===装饰器@property 的扩展的更多相关文章
- python基础——装饰器
python基础——装饰器 由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25 ...
- python基础—装饰器
python基础-装饰器 定义:一个函数,可以接受一个函数作为参数,对该函数进行一些包装,不改变函数的本身. def foo(): return 123 a=foo(); b=foo; print(a ...
- python 基础——装饰器
python 的装饰器,其实用到了以下几个语言特点: 1. 一切皆对象 2. 函数可以嵌套定义 3. 闭包,可以延长变量作用域 4. *args 和 **kwargs 可变参数 第1点,一切皆对象,包 ...
- day5学python 基础+装饰器内容
基础+装饰器内容 递归特性# 1.必须有一个明确的结束条件# 2.每次进入更深一层递归时,问题规模相比上次递归应有所减少# 3.递归效率不高 def run(n): print(n) if int(n ...
- python基础 (装饰器,内置函数)
https://docs.python.org/zh-cn/3.7/library/functions.html 1.闭包回顾 在学习装饰器之前,可以先复习一下什么是闭包? 在嵌套函数内部的函数可以使 ...
- <Python基础>装饰器的基本原理
1.装饰器 所谓装饰器一般是对已经使用(上线)的函数增加功能. 但是因为一般的大公司的严格按照开放封闭原则(对扩展是开放的,对修改是封闭的),不会让你修改原本的函数. 装饰器就是在不改变原本的函数且不 ...
- Python自动化 【第四篇】:Python基础-装饰器 生成器 迭代器 Json & pickle
目录: 装饰器 生成器 迭代器 Json & pickle 数据序列化 软件目录结构规范 1. Python装饰器 装饰器:本质是函数,(功能是装饰其它函数)就是为其他函数添加附加功能 原则: ...
- python基础-装饰器,生成器和迭代器
学习内容 1.装饰器 2.生成器 3.迭代器 4.软件目录结构规范 一:装饰器(decorator) 1.装饰器定义:本质就是函数,用来装饰其他函数,即为其他函数添加附加功能. 2.装饰器原则:1)不 ...
- python基础-装饰器
一.什么是装饰器 装饰器本质就是函数,功能是为其他函数附加功能 二.装饰器遵循的原则 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 三.实现装饰器的知识储备 装饰器=高阶函数+函数嵌套 ...
随机推荐
- 源码安装和yum安装的区别。
yum是将yum源中别人已经编译好的rpm包下载到本地,然后安装,不需要考虑依赖,主要是方便.源码安装没法人为的控制,安装的版本也很低. 源码安装需要自己编译,安装,编译过程中可以设置参数.可安装的版 ...
- [BZOJ2067]szn
description BZOJ权限题. solution 一道非常好的二分+贪心题目. 第一问就是\(\frac{\sum_u(deg[u]-1)}{2}+1\). 第二问需要在方案最优的情况下最长 ...
- POJ3347:Kadj Squares——题解
http://poj.org/problem?id=3347 题目大意:给定一些正方形的边长,让他们尽可能向左以45°角排列(不能互相重合),求在上面看只能看到哪几个正方形. ———————————— ...
- POJ2724:Purifying Machine——题解
http://poj.org/problem?id=2724 描述迈克是奶酪工厂的老板.他有2^N个奶酪,每个奶酪都有一个00 ... 0到11 ... 1的二进制数.为了防止他的奶酪免受病毒侵袭,他 ...
- 洛谷 P3380 bzoj3196 Tyvj1730 【模板】二逼平衡树(树套树)
[模板]二逼平衡树(树套树) 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作: 查询k在区间内的排名 查询区间内排名为k的值 修改某一位值上的数值 查询k在 ...
- warning: React does not recognize the xxx prop on a DOM element
这是React不能识别dom元素上的非标准attribute报出的警告,最终的渲染结果中React会移除这些非标准的attribute. 通常{...this.props}和cloneElement( ...
- bzoj1257: [CQOI2007]余数之和sum(数论)
非常经典的题目... 要求 则有 实际上 最多只有2*sqrt(k)种取值,非常好证明 因为>=sqrt(k)的数除k下取整得到的数一定<=sqrt(k),而k除以<=sqrt(k) ...
- Pycharm中一些不为人知的技巧
工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面实用技巧,能带来事半功倍的效果. 以下操作都是基于 Wind ...
- mybatis中parameterType可以写的别名
mybatis中parameterType可以写的别名 https://blog.csdn.net/sdzhangshulong/article/details/51749807 _byte byte ...
- 在某OC字符串中,搜索指定的某字符串:-rangeOfString:
NSString *originalStr = @"搜索:王者拜仁!"; NSString *subStr = @"搜索:"; // 在originalStr这 ...