python中的@property
@property 可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/getter也是需要的
class People:
def __init__(self,name,weight,height):
self.__name=name
self.weight=weight
self.height=height
@property
def bmi(self):
return self.weight / (self.height**2)
@bmi.deleter
def bmi(self):
del self.__name p1=People('wang',67,1.7)
del p1.bmi
print(p1.__name)
@bmi.deleter相当于一个接口,想要直接删除私有属性是不可以的,要有这么一个接口.删除私有属性
说明:同一属性的三个函数名要相同。(例子中都是bim)
@bmi.setter是修改
def bmi(self,新的参数)
classmethod
class Classmethod_Demo():
role = 'dog' @classmethod
def func(cls):
print(cls.role)
Classmethod_Demo.func()
staticmethod
class Staticmethod_Demo():
role = 'dog' @staticmethod
def func():
print("当普通方法用")
Staticmethod_Demo.func()
python中的@property的更多相关文章
- 关于python中的property
python中的property在类实例化的时候 可以把类方法变成类属性使用, 还可以用在简化赋值上 1)不用property的时候,你的类可能是这样写的 2)用propery的时候你可能会这样写,调 ...
- 第7.26节 Python中的@property装饰器定义属性访问方法getter、setter、deleter 详解
第7.26节 Python中的@property装饰器定义属性访问方法getter.setter.deleter 详解 一. 引言 Python中的装饰器在前面接触过,老猿还没有深入展开介绍装饰 ...
- python中的property属性
目录 1. 什么是property属性 2. 简单的实例 3. property属性的有两种方式 3.1 装饰器方式 3.2 类属性方式,创建值为property对象的类属性 4. property属 ...
- python中的property
提示:这篇博文参考了两个博客,第一篇博文地址为:https://www.cnblogs.com/Lambda721/p/6132206.html,另一篇博文地址如下:关于python的property ...
- Python中的@property装饰器
要了解@property的用途,首先要了解如何创建一个属性. 一般而言,属性都通过__init__方法创建,比如: class Student(object): def __init__(self,n ...
- python 中的property
""" property() 的第一个参数是 getter 方法,第二个参数是 setter 方法 xx = property(a,b) @property #用于指示g ...
- python中使用@property
class Student(object): @property def score(self): return self._score @score.setter def score(self, v ...
- 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...
- python中封装
封装 引子 从封装的本身意思去理解,封装就是用一个袋子,把买的水果.书.水杯一起装进袋子里,然后再把袋子的口给封上,照这样的理解来说,封装=隐藏,但是,这种理解是片面的 ## 如何封装 在python ...
随机推荐
- Oracle使用正则表达式拆分字段里多行分布式值
不规范的表设计往往会带来程序设计上的麻烦,也会降低SQL的性能. 例如下表显示的内容: 这样我们调取多值字段用来做匹配的话就比较麻烦,我们可以通过正则表达式REGEXP_SUBSTR先将 多值得列分成 ...
- Window、Linux查看本机外网ip
前言 我们上网用的IP并不一定是本机网卡的IP地址,由于公网IP地址稀少,国内绝大多数电脑上网,一般都是通过拨号或者端口映射.多个内网地址映射到一个公网地址来实现上网的. 目录 1.查看本机网卡ip ...
- python正则之特殊表达式 .*?{}
. 能匹配所有字符--单个字符,除了\n >>> re.match(r".","1") <_sre.SRE_Match object a ...
- legend3---用Homestead配置后报错“No input file specified.”
legend3---用Homestead配置后报错“No input file specified.” 一.总结 一句话总结: 自己项目上传到github的时候多增加了一层legend3的github ...
- nginx检查报错:nginx: [emerg] "server" directive is not allowed here in
想检查一个配置文件是否正确,-c 指定之后发现有报错,如下: [root@op-2:~# nginx -t -c /etc/nginx/conf.d/default.conf nginx: [emer ...
- Oracle数据库用户的密码过期问题处理
SQL> select username, user_id, account_status,expiry_date, profile from dba_users where username ...
- 将项目发布到neuxs私服
需要在 pom.xml中配置 <distributionManagement> <repository> <id>user-release</id> & ...
- leetcode 138. Copy List with Random Pointer复杂链表的复制
python代码如下: # Definition for singly-linked list with a random pointer. # class RandomListNode(object ...
- tomcat打开失败原因
我重装系统以后,tomcat无法打开,原因是有的项目的虚拟路径有误 到server.xml下修改虚拟路径
- Unity ZTest 深度测试 & ZWrite 深度写入
初学Shader,一开始对于渲染队列,ZTest 和 ZWrite一头雾水,经过多方查阅和实验,有了一些自己的理解.发此文与初学Shader的朋友分享,也算是为自己做个笔记.不对或不足之处欢迎指正. ...