python装饰器 property_python @property装饰器
1.通过@property装饰器,可以直接通过方法名来访问方法,不需要在方法名后添加一对“()”小括号。
class Person:
def __init__(self, name):
self.__name = name
@property
def say(self):
return self.__name
xioabai = Person("xiaobai")
#直接通过方法名来访问say方法
print("我的名字是:", xiaobai.say)
我的名字是: xiaobai
上面程序中,使用@property修饰了say()方法,这就使得该方法变成了name属性的getter方法。
2.使用setter装饰器,可以为say方法添加setter方法。
class Person:
def __init__(self, name):
self.__name = name
@property
def say(self):
return self.__name
@say.setter
def say(self, value):
self.__name = value
xiaobai = Person("xiaobai")
xiaobai.say = "xiaohei"
print("我的名字其实是:", xiaobai.say)
我的名字其实是: xiaohei
3.使用deleter装饰器来删除指定属性,可以为say方法添加deleter方法
class Person:
def __init__(self, name):
self.__name = name
@property
def say(self):
return self.__name
@say.setter
def say(self, value):
self.__name = value
@say.deleter
def say(self):
self.__name = "xxx"
xiaobai = Person("xiaobai")
del xiaobai.say
print("我的名字其实是:", xiaobai.say)
我的名字其实是: xxx
python装饰器 property_python @property装饰器相关推荐
- python私有属性和property装饰器_python – 在使用@property装饰器时在属性的s...
尝试覆盖子类中的属性时,我对此行为有点困惑. 第一个示例设置两个类,Parent和Child. Parent继承自object,而Child继承自Parent.属性a使用属性装饰器定义.调用child ...
- python装饰器 property_介绍Python的@property装饰器的用法
在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...
- [转载] python学习笔记——@property装饰器
参考链接: Python @property装饰器 上篇:https://blog.csdn.net/qq_42489308/article/details/89423412 @property装饰器 ...
- 介绍Python的@property装饰器的用法_python_脚本之家
介绍Python的@property装饰器的用法_python_脚本之家 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Studen ...
- pythonproperty装饰器_实例讲解Python编程中@property装饰器的用法
取值和赋值 class Actress(): def __init__(self): self.name = 'TianXin' self.age = 5 类Actress中有两个成员变量name和a ...
- python装饰器 property_Python中@property装饰器的使用技巧性解析(代码示例)
Python中@property装饰器的技巧性用法(代码示例) 本篇文章给大家带来的内容是关于Python中@property装饰器的技巧性用法(代码示例),有一定的参考价值,有需要的朋友可以参考一下 ...
- python装饰器 property_python中property和setter装饰器用法
作用:调用方法改为调用对象, 比如 : p.set_name() 改为 p.set_name 区别:前者改变get方法,后者改变set方法 效果图: 代码: class Person: def __i ...
- python内置装饰器property_python之内置装饰器(property/staticmethod/classmethod)
python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...
- python装饰器property_python装饰器: @property
原文链接:https://blog.csdn.net/YiJie__ShuSheng/article/details/84933648 1. 什么是property属性 一种用起来像是使用的实例属性一 ...
最新文章
- appium+python自动化33-解锁九宫格(TouchAction)
- Android核心程序之SystemUI - (一)开篇
- 云漫圈 | 什么是微服务?
- 较真的来了!这篇【硬核论文】为何恺明新作MAE提供了一种理论解释和数学证明...
- 基于HTTP的QQ协议(转)
- 不是三维——软件项目的设计、开发与管理
- 移动医疗的服务怎么做?来听听传统医疗器械厂商怎么说
- 明日服务器中断,明日之后服务器连接中断 无法登陆解决办法[多图]
- 软件项目详细设计方案怎么写?
- Vim开发RubyOnRails 环境打造
- Abbot和Marathon比较
- 谷歌大中华区总裁李开复离职
- 毕业季--写给未来的自己
- 断点续传 scp rsync
- 一个最简单的Delphi2010的PNG异形窗口方法
- Instability Analysis and Oscillation Suppression of Enhancement-Mode GaN Devices--GaN半桥电路中不稳定分析
- 开源之道给您拜年啦~~
- Leetcode_Weekly_310
- 螺旋传动设计系统lisp_螺旋传动设计.doc
- 临近年关,为何越来越多的程序员不愿回家,带你盘点那些程序员最怕的几件事
热门文章
- R构建层次聚类模型(Hierarchical Clustering)
- 对于图像分类任务,相对于全连接的DNN,CNN模型的主要优点有哪些?
- TED+肢体语言塑造你自己+power+fake it till you make it
- 深度学习多变量时间序列预测:卷积神经网络(CNN)算法构建时间序列多变量模型预测交通流量+代码实战
- Python3 函数function
- matlab的syms无法在函数中使用_Python函数中使用@
- pgsql 筛选中文字符正则_postgresql varchar字段regexp_replace正则替换
- vs2019 MFC 中 cannot open include file 'afxres.h' 问题解决方法
- 报错解决 :Couldn't find any package by regex 'g++-8'
- APUE(第九章)进程关系