property 装饰器的作用

property 装饰器将方法包装成属性,将私有属性公有化,此属性只能被读取。相当于实现get方法的对象

class People:
def __init__(self, identity_number):
self._identity_number = identity_number @property # 只读
def age(self):
return self._age @age.setter # 写
def age(self, value):
if not isinstance(value, int):
raise ValueError("age must be an integer!")
if value < 0:
raise ValueError("age must more than 0")
self._age = value @age.deleter # 删除
def age(self):
del self._age @property
def get_identity_number(self):
return self._identity_number #In [22]: p = People(123456) #In [23]: p.age = 18 # In [24]: p.age
# Out[24]: 18 # In [25]: del p.age # In [26]: p.age
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-26-3523b116dc0e> in <module>()
----> 1 p.age <ipython-input-21-de21f31a52ce> in age(self)
5 @property # 只读
6 def age(self):
----> 7 return self._age
8
9 @age.setter # 写 AttributeError: 'People' object has no attribute '_age' # In [27]: p.age = 18 # In [28]: p.age = 18.6
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-28-8a03211dcd49> in <module>()
----> 1 p.age = 18.6 <ipython-input-21-de21f31a52ce> in age(self, value)
10 def age(self, value):
11 if not isinstance(value, int):
---> 12 raise ValueError("age must be an integer!")
13 if value < 0:
14 raise ValueError("age must more than 0") ValueError: age must be an integer! # In [29]: p.age = '11'
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-29-b6d20eff2848> in <module>()
----> 1 p.age = '11' <ipython-input-21-de21f31a52ce> in age(self, value)
10 def age(self, value):
11 if not isinstance(value, int):
---> 12 raise ValueError("age must be an integer!")
13 if value < 0:
14 raise ValueError("age must more than 0") ValueError: age must be an integer! # In [30]: p.age = -1
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-30-47db6d5817ed> in <module>()
----> 1 p.age = -1 <ipython-input-21-de21f31a52ce> in age(self, value)
12 raise ValueError("age must be an integer!")
13 if value < 0:
---> 14 raise ValueError("age must more than 0")
15 self._age = value
16 ValueError: age must more than 0 # In [31]:

会把成员函数x转换为getter,相当于做了x = property(); x = x.getter(x_get)

  • @property表示只读。
  • 同时有@property和@x.setter表示可读可写。
  • 同时有@property和@x.setter和@x.deleter表示可读可写可删除。

参考资料:https://docs.python.org/3/library/functions.html?highlight=property#property

http://blog.willdx.me/web/面向对象进阶.html

内置装饰器二:@property的更多相关文章

  1. python基础语法16 面向对象3 组合,封装,访问限制机制,内置装饰器property

    组合: 夺命三问: 1.什么是组合? 组合指的是一个对象中,包含另一个或多个对象. 2.为什么要用组合? 减少代码的冗余. 3.如何使用组合? 耦合度: 耦: 莲藕 ---> 藕断丝连 - 耦合 ...

  2. property内置装饰器函数和@name.setter、@name.deleter

    # property # 内置装饰器函数 只在面向对象中使用 # 装饰后效果:将类的方法伪装成属性 # 被property装饰后的方法,不能带除了self外的任何参数 from math import ...

  3. python进阶04 装饰器、描述器、常用内置装饰器

    python进阶04 装饰器.描述器.常用内置装饰器 一.装饰器 作用:能够给现有的函数增加功能 如何给一个现有的函数增加执行计数的功能 首先用类来添加新功能 def fun(): #首先我们定义一个 ...

  4. python内置装饰器

    前言 接着上一篇笔记,我们来看看内置装饰器property.staticmethod.classmethod 一.property装饰器 1. 普通方式修改属性值 code class Celsius ...

  5. classmethod、staticclassmethod内置装饰器函数

    # method 英文是方法的意思 # classmethod 类方法 # 当一个类中的方法中只涉及操作类的静态属性时,此时在逻辑上,我们想要直接通过类名就可以调用这个方法去修改类的静态属性,此时可以 ...

  6. Python内置装饰器@property

    在<Python装饰器(Decorators )>一文中介绍了python装饰器的概念,日常写代码时有一个装饰器很常见,他就是内置的@property. 我们一步步的来接近这个概念. 一个 ...

  7. 面向对象——组合、封装、访问限制机制、property内置装饰器

    面向对象--组合.封装.访问限制机制.property 组合 什么是组合? 组合指的是一个对象中,包含另一个或多个对象 为什么要组合? 减少代码的冗余 怎么用组合? # 综合实现 # 父类 class ...

  8. python之内置装饰器(property/staticmethod/classmethod)

    python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...

  9. Python 内置装饰器

    内置的装饰器 ​ 内置的装饰器和普通的装饰器原理是一样的,只不过返回的不是函数,而是类对象,所以更难理解一些. @property ​ 在了解这个装饰器前,你需要知道在不使用装饰器怎么写一个属性. d ...

随机推荐

  1. C#通过反射获得对象所有属性和值

    C#获得对象的所有属性和值 public void GetPros() { UserInfo userInfo = new UserInfo(); userInfo.ID = ; userInfo.N ...

  2. Spring 学习记录3 ConversionService

    ConversionService与Environment的关系 通过之前的学习(Spring 学习记录2 Environment),我已经Environment主要是负责解析properties和p ...

  3. 2018.08.30 NOIP模拟 wall(模拟)

    [问题描述] 万里长城是中国强大的标志,长城在古代的用途主要用于快速传递军事消息和抵御 外敌,在长城上的烽火台即可以作为藏兵的堡垒有可以来点燃狼烟传递消息. 现在有一段 万里长城,一共有 N 个烽火台 ...

  4. 日期 时间选择器(DatePicker和TimePicker)实现用户选择

    日期和时间 作者的设计TimePicker时,大小分布不合理,我调整宽度为match-parent高度为wrap-parent就可以了. public class MainActivity exten ...

  5. LA 3942 && UVa 1401 Remember the Word (Trie + DP)

    题意:给你一个由s个不同单词组成的字典和一个长字符串L,让你把这个长字符串分解成若干个单词连接(单词是可以重复使用的),求有多少种.(算法入门训练指南-P209) 析:我个去,一看这不是一个DP吗?刚 ...

  6. MySQL性能调优与架构设计——第 18 章 高可用设计之 MySQL 监控

    第 18 章 高可用设计之 MySQL 监控 前言: 一个经过高可用可扩展设计的 MySQL 数据库集群,如果没有一个足够精细足够强大的监控系统,同样可能会让之前在高可用设计方面所做的努力功亏一篑.一 ...

  7. (连通图 模板题 出度和入度)Network of Schools--POJ--1236

    链接: http://poj.org/problem?id=1236 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#probl ...

  8. Codeforces777C Alyona and Spreadsheet 2017-05-04 17:46 103人阅读 评论(0) 收藏

    C. Alyona and Spreadsheet time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Win7_Ultimate + VS2010 + openGL_MFC单文档应用开发框架搭建步骤

    Win7_Ultimate + VS2010 + openGL单文档应用开发框架搭建步骤 上一个配置是基于OpenGL的开发工具配置的,下面就是基于Vs2010的MFC单文档应用开发. 通过网上查找资 ...

  10. C# Timer类

    C# 有三种不同的Timer类 1.Threading.Timer 2.Timer.Timer 3.Forms.Timer using System; using System.Collections ...