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 ...
随机推荐
- 我写过的bug...
添加对,更新和查询出错 因为只写了bean里只写了se't方法,没有写get 多条件联合查询,后一个条件没用 写xml里面复制上一行,忘记把变量名改了(propertyValue没改成register ...
- 原生JS去重
方式一: function deleteRepetionChar(arr){ //先判断输入进来的是数组对象还是字符串 if( typeof arr == "object"){ v ...
- 前端必须掌握的 docker 技能(3)
概述 作为一个前端,我觉得必须要学会使用 docker 干下面几件事: 部署前端应用 部署 nginx 给部署的 nginx 加上 https 使用 docker compose 进行部署 给 ngi ...
- log() exp()函数
1 对数函数表示法 import numpy as np import math print('输出自然底数e:',math.e) # np表示法 # np.log()是以e为底的自然对数 print ...
- mysql 5.5 安装教程
(转自:https://www.cnblogs.com/solargen/p/6835399.html) 1. 官网下载mysql5.5 下载地址: http://dev.mysql.com/down ...
- Gradle之Android Gradle Plugin 主要流程分析(二)
[Android 修炼手册]Gradle 篇 -- Android Gradle Plugin 主要流程分析 预备知识 理解 gradle 的基本开发 了解 gradle task 和 plugin ...
- JavaScript —— 用法 输出
用法 必须在<script>标签内 可以在<body>和<head>部分中 不限数量 <script> ... </script> 即可,不 ...
- Cocos2d-X网络编程(2) Cocos2d中的网络通信协议——http协议
HTTP协议也叫超文本传输协议.是互联网广泛使用的通信协议,常用于B/S架构中. HTTP连接使用的是短连接形式,也就是"请求-响应"的方式,不仅在请求时需要先建立连接,而且需要客 ...
- mysql数据库自带数据库介绍
show databases:查看mysql自带数据库有information_schema,mysql, performance_schema, test information_schema数据库 ...
- linux 文件属性文件权限
权限 -rw-------. root root Mar : anaconda-ks.cfg drwxr-xr-x root root May : dir1 drwxr-xr-x root root ...