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 ...
随机推荐
- [LeetCode]-DataBase-Combine Two Tables
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- CentOS关闭系统不必要的端口
注:以下所有操作均在CentOS 7.2 x86_64位系统下完成. 1)首先查看当前系统开放的端口号: # netstat -tlnup Active Internet connections (o ...
- POST上传多张图片配合Django接受多张图片
POST上传多张图片配合Django接受多张图片 本地:POST发送文件,使用的是files参数,将本地的图片以二进制的方式发送给服务器. 在这里 files=[("img",op ...
- Mac securecrt 破解版安装
破解一 1.先链接:https://pan.baidu.com/s/1-1nu4eRf7BmuLg5MtlCRvw 密码:30pq 默认下载到了当前用户的”下载”目录中 在”Finder”中 ...
- KVM 记录
mkdir -p /home/hugepagesmount -t hugetlbfs hugetlbfs /home/hugepages 配置文件 vim /etc/libvirt/qemu.conf ...
- 神经网络学习笔记一——Neural Network
参考自http://deeplearning.stanford.edu/wiki/index.php/Neural_Networks 神经元模型 h(x)= f(W'x)f(z)一般会用sigmoid ...
- 怎么用jira写bug
工具/原料 有网的电脑 方法/步骤1: 打开公司给的访问JIRA的链接,输入公司给你注册的账号和密码,点击登录 方法/步骤2: 点击JIRA主菜单上的“创建”,进入编辑bug界面 方法/步骤3: 项目 ...
- php5.6编译安装apache
1.下载源码包wget 网址/php-5.6.30.tar.gz2.解压源码包tar -zxvf php-5.6.30.tar.gz3.创建一个安装目录mkdir /usr/local/php4.进入 ...
- Linux iptables 防火墙常用规则
iptables 安装 yum install iptables iptables 规则清除 iptables -F iptables -X iptables -Z 开放指定的端口允许本地回环接口(即 ...
- win10在文件夹下打开powershell
快捷键win+R,输入cmd可以直接打开终端命令行窗口 在文件夹下打开终端命令行端口: 在需要的文件夹目录下,按住shift键,在空白处右击,选择在此处打开powershell窗口,即可进行终端命令行 ...