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装饰器相关推荐

  1. python私有属性和property装饰器_python – 在使用@property装饰器时在属性的s...

    尝试覆盖子类中的属性时,我对此行为有点困惑. 第一个示例设置两个类,Parent和Child. Parent继承自object,而Child继承自Parent.属性a使用属性装饰器定义.调用child ...

  2. python装饰器 property_介绍Python的@property装饰器的用法

    在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Student() s.score = 9999 这显然不合逻辑.为了限制score的 ...

  3. [转载] python学习笔记——@property装饰器

    参考链接: Python @property装饰器 上篇:https://blog.csdn.net/qq_42489308/article/details/89423412 @property装饰器 ...

  4. 介绍Python的@property装饰器的用法_python_脚本之家

    介绍Python的@property装饰器的用法_python_脚本之家 在绑定属性时,如果我们直接把属性暴露出去,虽然写起来很简单,但是,没办法检查参数,导致可以把成绩随便改: s = Studen ...

  5. pythonproperty装饰器_实例讲解Python编程中@property装饰器的用法

    取值和赋值 class Actress(): def __init__(self): self.name = 'TianXin' self.age = 5 类Actress中有两个成员变量name和a ...

  6. python装饰器 property_Python中@property装饰器的使用技巧性解析(代码示例)

    Python中@property装饰器的技巧性用法(代码示例) 本篇文章给大家带来的内容是关于Python中@property装饰器的技巧性用法(代码示例),有一定的参考价值,有需要的朋友可以参考一下 ...

  7. python装饰器 property_python中property和setter装饰器用法

    作用:调用方法改为调用对象, 比如 : p.set_name() 改为 p.set_name 区别:前者改变get方法,后者改变set方法 效果图: 代码: class Person: def __i ...

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

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

  9. python装饰器property_python装饰器: @property

    原文链接:https://blog.csdn.net/YiJie__ShuSheng/article/details/84933648 1. 什么是property属性 一种用起来像是使用的实例属性一 ...

最新文章

  1. appium+python自动化33-解锁九宫格(TouchAction)
  2. Android核心程序之SystemUI - (一)开篇
  3. 云漫圈 | 什么是微服务?
  4. 较真的来了!这篇【硬核论文】为何恺明新作MAE提供了一种理论解释和数学证明...
  5. 基于HTTP的QQ协议(转)
  6. 不是三维——软件项目的设计、开发与管理
  7. 移动医疗的服务怎么做?来听听传统医疗器械厂商怎么说
  8. 明日服务器中断,明日之后服务器连接中断 无法登陆解决办法[多图]
  9. 软件项目详细设计方案怎么写?
  10. Vim开发RubyOnRails 环境打造
  11. Abbot和Marathon比较
  12. 谷歌大中华区总裁李开复离职
  13. 毕业季--写给未来的自己
  14. 断点续传 scp rsync
  15. 一个最简单的Delphi2010的PNG异形窗口方法
  16. Instability Analysis and Oscillation Suppression of Enhancement-Mode GaN Devices--GaN半桥电路中不稳定分析
  17. 开源之道给您拜年啦~~
  18. Leetcode_Weekly_310
  19. 螺旋传动设计系统lisp_螺旋传动设计.doc
  20. 临近年关,为何越来越多的程序员不愿回家,带你盘点那些程序员最怕的几件事

热门文章

  1. R构建层次聚类模型(Hierarchical Clustering)
  2. 对于图像分类任务,相对于全连接的DNN,CNN模型的主要优点有哪些?
  3. TED+肢体语言塑造你自己+power+fake it till you make it
  4. 深度学习多变量时间序列预测:卷积神经网络(CNN)算法构建时间序列多变量模型预测交通流量+代码实战
  5. Python3 函数function
  6. matlab的syms无法在函数中使用_Python函数中使用@
  7. pgsql 筛选中文字符正则_postgresql varchar字段regexp_replace正则替换
  8. vs2019 MFC 中 cannot open include file 'afxres.h' 问题解决方法
  9. 报错解决 :Couldn't find any package by regex 'g++-8'
  10. APUE(第九章)进程关系