python property装饰器
直接上代码:
#!/usr/bin/python
#encoding=utf-8 """
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式
""" class Parrot:
#class Parrot(object): #报错,AttributeError: can't set attribute'
def __init__(self):
self._voltage = 10000 @property
def voltage(self):
return self._voltage if __name__ == "__main__":
p = Parrot()
print p.voltage #就这样要调用函数,任性
p.voltage = 12 #给函数赋值,python中函数也是对象
print p.voltage
root@u163:~/cp/python# python property.py
10000
12
python property装饰器的更多相关文章
- Python的property装饰器的基本用法
Python的@property装饰器用来把一个类的方法变成类的属性调用,然后@property本身又创建了另一个装饰器,用一个方法给属性赋值.下面是在类中使用了@property后,设置类的读写属性 ...
- [转载]Python使用@property装饰器--getter和setter方法变成属性
原贴:为什么Python不需要getter和setter getter 和 setter在java中被广泛使用.一个好的java编程准则为:将所有属性设置为私有的,同时为属性写getter和sette ...
- 【python】@property装饰器
Python内置的@property装饰器可以把类的方法伪装成属性调用的方式.也就是本来是Foo.func()的调用方法,变成Foo.func的方式.在很多场合下,这是一种非常有用的机制. class ...
- python staticmethod,classmethod方法的使用和区别以及property装饰器的作用
class Kls(object): def __init__(self, data): self.data = data def printd(self): print(self.data) @st ...
- python中@property装饰器的使用
目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...
- python面向对象:组合、封装、property装饰器、多态
一.组合二.封装三.property装饰器四.多态 一.组合 ''' 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. ...
- python面向编程:类的组合、封装、property装饰器、多态
一.组合 二.封装 三.propert装饰器 四.多态 一.组合 ''' 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 ...
- 第7.27节 Python案例详解: @property装饰器定义属性访问方法getter、setter、deleter
上节详细介绍了利用@property装饰器定义属性的语法,本节通过具体案例来进一步说明. 一. 案例说明 本节的案例是定义Rectangle(长方形)类,为了说明问题,除构造函数外,其他方法都只 ...
- 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解
第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一. 引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...
随机推荐
- C++设计模式之装饰者模式
#include "HandCake.h" //手抓饼 HandCake::HandCake() { ; this->name="手抓饼"; } Hand ...
- Httpwatch 工具介绍
一 概述: HttpWatch强大的网页数据分析工具.集成在Internet Explorer工具栏.包括网页摘要.Cookies管理.缓存管理.消息头发送/接受.字符查询.POST 数据和目录管理功 ...
- Get Intellisense for .axml files in Visual Studio
原文Get Intellisense for .axml files in Visual Studio So in order to get some intellisense support for ...
- 微信获取用户数据后台写法,author2.0认证
/* 微信授权接口 */ //1.设置路由 router.get('/wechat/userinfo', function(req, res) { var cb = req.query.cb; //设 ...
- HDU 2254 奥运(数论+矩阵)
题目中文的不解释啊. .. 须要注意的就是:离散数学中,有向图的邻接矩阵A表示全部点之间路径长度为1的路径数量,A^n则表示路径长度为n的路径数量.故须要求某两点在(A^t1)~(A^t2)的路径数量 ...
- loading android
drawal/loading.xml <?xml version="1.0" encoding="utf-8"?><animated-rota ...
- Xposed出现 java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation
Xposed出现 java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implem ...
- C++中#include的工作原理
大多数人可能对“#include”比较熟悉,因为我们写C/C++程序的时候都会写的字符串之一,但是它是具体怎么工作的?或者它的原理是什么呢? 可能不太熟悉,也有可能没有去关心过.我们只关心程序能否正确 ...
- leetcode Reverse Integer python
class Solution(object): def reverse(self, x): """ :type x: int :rtype: int "&quo ...
- 数据库分页【Limt与Limt..OFFSET 】
数据起始 SELECT * from xiaoyao_blogs_essay limit 20 , 15;解释:20是起始位置,15是页容量.因为id是从15开始的 SELECT * from xi ...