@property 装饰器
property() 函数作用于新式类,返回属性值。
class C(object):
def __init__(self):
self._x = None def getx(self):
print('get')
return self._x def setx(self, value):
print('set')
self._x = value def delx(self):
print('del')
del self._x x = property(getx, setx, delx, "I'm the 'x' property.") # c = C()
# c.x = 12
# print(c.x)
# del c.x
@property 装饰器用的最多。把一个方法变成属性调用,那么我们在对实例属性操作的时候,就知道该属性很可能不是直接暴露的,而是通过getter和setter方法来实现的。
# @property 装饰器负责把一个方法变成属性调用 class Student(object):
# 把一个getter方法变成属性,只需加上 @property
@property
def score(self):
return self._score # 把一个setter方法变成属性,@fun.setter
@score.setter
def score(self, value):
if not isinstance(value, int):
raise ValueError('score must be an interger!')
if value < 0 or value > 100:
raise ValueError('score must between 0~100')
self._score = value s = Student()
s.score = 60
print(s.score)
class Parrot(object):
def __init__(self):
self._voltage = 100000 @property
def voltage(self):
"""Get the current voltage."""
return self._voltage # setter,deleter,这些额外函数名和property相同
@voltage.setter
def voltage(self, value):
self._voltage = value @voltage.deleter
def voltage(self):
del self._voltage
参考:
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143186781871161bc8d6497004764b398401a401d4cce000
http://www.runoob.com/python/python-func-property.html
@property 装饰器的更多相关文章
- Python的property装饰器的基本用法
Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...
- 面向对象之组合、封装、多态、property装饰器
概要: 组合 封装 property装饰器 多态 Python推崇鸭子类型:解耦合,统一标准(不用继承) 1. 组合 继承:会传递给子类强制属性 组合:解耦合,减少占用内存.如:正常继承,如果一个班级 ...
- import导入模块,==和is,浅拷贝和深拷贝,进制转换,位运算,私有化,property装饰器
'''import导入模块'''import sysprint(sys.path) sys.path.append('D://ASoft/Python/PycharmProjects')import ...
- property装饰器
# 需要了解的property的用法 class People: def __init__(self,name): self.__name=name @property def name(self): ...
- 组合,多态与多态性,封装以及property装饰器介绍
一:组合: 什么是组合:组合指的是某一个对象拥有一个属性,该属性的值是另外一个类的对象. 为何要用组合:通过为某一个对象添加属性(属性的值是另外一个类的对象)的方式,可以间接地将两个类关联/整合/组合 ...
- Python10/24--组合/封装/property装饰器/多态
组合的应用: 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. 如何用组合 '''class Foo: aaa=111 ...
- 手动实现property装饰器
首先,property装饰器是通过数据描述符实现的.用法很简单,大家应该都知道,这里就不细说了. 这里主要分析一下property是如何通过描述符实现的. class Property: def __ ...
- PYTHON-组合 封装 多态 property装饰器
# 组合'''软件重用的重要方式除了继承之外还有另外一种方式,即:组合组合指的是,在一个类中以另外一个类的对象作为数据属性,称为类的组合 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之 ...
- [转载]Python使用@property装饰器--getter和setter方法变成属性
原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...
- 面向对象之 组合 封装 多态 property 装饰器
1.组合 什么是组合? 一个对象的属性是来自另一个类的对象,称之为组合 为什么要用组合 组合也是用来解决类与类代码冗余的问题 3.如何用组合 # obj1.xxx=obj2''''''# class ...
随机推荐
- sql 解释顺序
from:全量数据, where:数据过滤,生成新的虚表.个人主观上理解,where中的条件,如果涉及到join中的表,则会移动到相应的on条件中,减少后续生成的虚表大小. join:根据on中的条件 ...
- SpringBoot Mybatis的驼峰命名
开启驼峰命名的方法 第一种方式: 可以在配置类中进行配置.配置的Demo如下: @Bean(name="sqlSessionFactory") public SqlSessionF ...
- 四、cent OS安装配置mysql
下载mysql的repo源wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm 安装mysql-community-r ...
- groovy函数、字符串、循环
三重单引号字符串 '''a triple single quoted string''' 三重单引号字符串是普通的java.lang.String 三重单引号字符串是多行的.您可以跨越行边界跨越字符串 ...
- HDU 2045 RPG难题
http://acm.hdu.edu.cn/showproblem.php?pid=2045 这道题也是用倒推: 先假设前n-2个块都已经涂好,涂第n-1块时有以下两种情况: 1.n-1和1相同,则n ...
- sublime设置语法自动整齐快捷键技巧
preference>>key bindings-user>>编辑设置文档, 输入 { "keys": ["ctrl+q"], &quo ...
- How To Secure Apache with Let's Encrypt on Ubuntu (Free SSL)
Introduction This tutorial will show you how to set up a TLS/SSL certificate from Let's Encrypt on a ...
- python学习笔记之——正则表达式
1.re模块 Python通过re模块提供对正则表达式的支持,re 模块使 Python 语言拥有全部的正则表达式功能.使用re的一般步骤是先将正则表达式的字符串形式编译为Pattern实例,然后使用 ...
- 一步一步pwn路由器之路由器环境修复&&rop技术分析
前言 本文由 本人 首发于 先知安全技术社区: https://xianzhi.aliyun.com/forum/user/5274 拿到路由器的固件后,第一时间肯定是去运行目标程序,一般是web服务 ...
- js 捕捉回车键触发登录,并验证输入内容
js 捕捉回车键触发登录,并验证输入内容 有时候我们会遇到 web 页面中捕捉按键,触发一些效果, 比如常见的回车键触发登录,并验证输入内容,下面会介绍,截图: 一.最简单的捕捉回车键:判断按下的是不 ...