面向对象_内置函数 property
property
将方法伪装成为属性,可以不用加上()就可以调出其属性。
但是用__dict__,不能调出此属性
from math import pi
class Circle:
def __init__(self,r):
self.r = r
@property
def perimeter(self):
return 2*pi*self.r
@property
def area(self):
return self.r**2*pi c1 = Circle(5)
print(c1.area) # 圆的面积
print(c1.perimeter) # 圆的周长
property
property有2个配合使用的函数
property可以和setter,delecter配合使用,已达到get,set,delect的功能
class Stuff_price:
def __init__(self,original_price,discount):
self.__original_price=original_price
self.__discount=discount
@property
def price(self):
print('我执行了了价格')
return self.__original_price*self.__discount
@price.setter
def price(self,new_discount):
print('我执行了setter')
self.__discount=new_discount
@price.deleter
def price(self):
print('我执行了 delecte')
# del self.__original_price
apple=Stuff_price(10,0.5)
print(apple.price)
apple.price=0.8
print(apple.price)
del apple.price
print(apple.price)
'''输出结果我执行了了价格
5.0
我执行了setter
我执行了了价格
8.0
我执行了 delecte
我执行了了价格
8.0
'''
解析

注意del 并不是表示去删除,而是表示,去操作方法的引子。
这样便实现了,正常的属性的调法
面向对象_内置函数 property的更多相关文章
- python之类的多态(鸭子类型 )、封装和内置函数property
一.多态 1.什么是多态:一个类表现出的多种状态--->通过继承来实现的例如:class Animal:passclass Dog(Animal):passclass Cat(Animal):p ...
- python基础12_匿名_内置函数
一个二分查找的示例: # 二分查找 示例 data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35, 36, ...
- Python笔记(二十一)_内置函数、内置方法
内置函数 issubclass(class1,class2) 判断class1类是否为class2类的子类,返回True和False 注意1:类会被认为是自身的子类 >>>issub ...
- python成长之路第三篇(3)_内置函数及生成器迭代器
打个广告欢迎加入linux,python资源分享群群号:478616847 目录: 1.lambda表达式 2.map内置函数 3.filter内置函数 4.reduce内置函数 5.yield生成器 ...
- 面向对象进阶------>内置函数 str repr new call 方法
__new__方法: 我们来讲个非常非常重要的内置函数和init一样重要__new__其实在实例话对象的开始 是先继承父类中的new方法再执行init的 就好比你生孩子 先要把孩子生出来才能对孩子 ...
- Python基础(10)_内置函数、匿名函数、递归
一.内置函数 1.数学运算类 abs:求数值的绝对值 divmod:返回两个数值的商和余数,可用于计算页面数 >>> divmod(5,2) (2, 1) max:返回可迭代对象中的 ...
- Python笔记(五)_内置函数BIF
查看所有的内置函数:dir(__builtins__) abs() 获取绝对值 max() 返回给定元素中的最大值 min() 返回给定元素中的最小值 sum() 求和 reverse ...
- 2.9高级变量类型操作(列表 * 元组 * 字典 * 字符串)_内置函数_切片_运算符_for循环
高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...
- Python内置函数property()使用实例
class Shuxing(): def __init__(self, size = 10): self.size = size def getSize(self): print('getSize') ...
随机推荐
- 记Javascript一道题的理解
代码如下: function Foo(){ getName = function(){ console.log("1"); } return this; } Foo.getName ...
- PYTHON BS 四大对象
BeautifulSoup是灵活又方便的网页解析库,处理搞笑,支持多种解析器利用它不用编写正则表达式即可方便地实现网页信息的提取BS的四大对象:1.TagTag就是HTML中的一个个标签,例如:< ...
- Classifying with k-Nearest Neighbors(k近邻)
终于要开始写自己的第一篇博客啦,真有点小激动(手足无措 =.=!).因为最近正在琢磨机器学习,第一篇博客就从学的第一个算法开始:k-nearest neighbors algorithm即k近邻算法. ...
- Java 8的用法(泛型接口,谓词链)
1.泛型接口 我们举个例子,以前来看一下JPA定义的写法: Specification接口为: public interface Specification<T> { Predicate ...
- HashMap,HashTable,ConcurrentHashMap异同比较
0. 前言 HashMap和HashTable的区别一种比较简单的回答是: (1)HashMap是非线程安全的,HashTable是线程安全的. (2)HashMap的键和值都允许有null存在,而H ...
- Dapper批量操作实体
首先要安装 Dapper.Contrib,直接从nuget安装即可. dapper官方文档:http://dapper-tutorial.net/dapper T Get<T>(id); ...
- angular懒加载
生成module和routing.module文件 {//路径 path: 'InfectionFillInComponent', loadChildren: './component/his/inf ...
- nginx sub模块替换文本
nginx的ngx_http_sub_module模块,可以用于修改网站响应内容中的字符串,如过滤敏感词.第三方模块ngx_http_substitutions_filter_module,弥补了ng ...
- python 图片在线转字符画预览
文章链接:https://mp.weixin.qq.com/s/yiFOmljhyalE8ssAgwo6Jw 关于python图片转字符画,相信大家都不陌生,经常出现在 n个超有趣的python项目中 ...
- ionic3 Toast组件
html页面 <button ion-button color="dark" class="button-block" (click)="sho ...