python中@property和property函数使用
1、基本的@property使用,可以把函数当做属性用
- class Person(object):
- @property
- def get_name(self):
- print('我叫xxx')
- def main():
- person = Person()
- person.get_name
- if __name__ == '__main__':
- main()
2、@property的set,deleter,get
- class Goods(object):
- @property
- def price(self):
- print('@property')
- @price.setter
- def price(self,value):
- print('@price.setter:'+str(value))
- @price.deleter
- def price(self):
- print('@price.deleter')
- obj = Goods()
- obj.price = 50
- obj.price
- del obj.price
3、@property demo
- class Goods(object):
- def __init__(self):
- #原价
- self.original_price = 100
- #折扣
- self.discount = 0.8
- @property
- def price(self):
- #实际价格=原价*折扣
- new_price = self.original_price*self.discount
- return new_price
- @price.setter
- def price(self,value):
- self.original_price = value
- @price.deleter
- def price(self):
- del self.original_price
- obj = Goods()
- obj.price
- obj.price = 200
- del obj.price
4、property函数使用
- class Foo(object):
- def get_name(self):
- print('get_name')
- return 'laowang'
- def set_name(self, value):
- '''必须两个参数'''
- print('set_name')
- return 'set value' + value
- def del_name(self):
- print('del_name')
- return 'laowang'
- NAME = property(get_name, set_name, del_name, 'description.')
- obj = Foo()
- obj.NAME #调用get方法
- obj.NAME = 'alex' #调用set方法
- desc = Foo.NAME.__doc__ #调用第四个描述
- print(desc)
- del obj.NAME #调用第三个删除方法
5、property函数操作私有属性的get和set方法
- class Person(object):
- def __init__(self, age):
- self.__age = age
- def set_age(self, value):
- self.__age = value
- def get_age(self):
- return self.__age
- AGE = property(get_age, set_age)
- person = Person(15)
- person.AGE = 20
- print(str(person.AGE))
python中@property和property函数使用的更多相关文章
- Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted
1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...
- Python中的高阶函数与匿名函数
Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...
- python中enumerate()函数用法
python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6] 请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...
- Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用
Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...
- Python中sort和sorted函数代码解析
Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...
- Python中进制转换函数的使用
Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...
- 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析
一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...
- Python中的内置函数
2.1 Built-in Functions The Python interpreter has a number of functions built into it that are alway ...
- Python中str()与repr()函数的区别
在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...
- python中os.path.isdir()函数的使用
在python 中,os.path.isdir(path)函数主要用来判断函数内部的path是否为一个目录 具体关于这个函数的解说参考博客https://blog.csdn.net/xjp_xujip ...
随机推荐
- memcache分布式 存取
Memcached分布式 Memcached虽然称为“分布式“缓存服务器,但服务器端并没有“分布式”的功能.Memcached的分布式完全是由客户端实现的.memcached是怎么实现分布式缓存的呢? ...
- 高可用api接口网络部署方案
我们平时接触的产品都是7*24小时不间断服务,产品中的api接口肯定也是高可用的,下面我向大家分享一下互联网公司api接口高可用的网络部署方案. 我们一般通过http://le.quwenzhe.c ...
- ORACLE默认实例设置--linux
数据库实例多了之后,每次export的时候,显示的ORACLE_SID总不是自己经常用的那个,要是能让默认的自定义就好了. 现在就解释一下在linux环境中如何定义: 1.su - oracle 2. ...
- python第十一课——转换结构
3.转换函数:int():float():str():list():tuple():set():dict():bool(): 案例: #演示各个转换函数的使用: 数值型-->字符型使用:str( ...
- CF993E:Nikita and Order Statistics(FFT)
Description 给你一个数组 $a_{1 \sim n}$,对于 $k = 0 \sim n$,求出有多少个数组上的区间满足:区间内恰好有 $k$ 个数比 $x$ 小.$x$ 为一个给定的数. ...
- Day4 数组
双重for循环 外循环控制行,内循环控制列. //乘法表 ; i <= ; i++) { ; j <= i ;j++) { System.out.print(j+"*" ...
- 最简单的php学习
php文件操作函数 filetype()判断文件的基本类型 egg 目录文件 文件 等 dir文件夹 file 文件 stat()函数获得指定文件名参数目标文件的基本属性 在php中以is_开头 ...
- DOS命令行简单用法
DOS命令行简单用法 基本用法 1.cd(回车)从当前目录切回到根目录. 2.dir(回车)列出当前目录下的所有文件. 3.md kkk(回车)在当前目录下创建一个名称为kkk的文件夹. 4.rd k ...
- Spring源码分析(十二)FactoryBean的使用
摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 一般情况下,Spring通过反射机制利用bean的class属性指定实现 ...
- dpkg安装失败解决过程
终于好了.搞到转钟3点都没搞定,耽误不少时间. 执行sudo port install dpkg 报错如下Error: org.macports.build for port gmp return ...