1、基本的@property使用,可以把函数当做属性用

  1. class Person(object):
  2. @property
  3. def get_name(self):
  4. print('我叫xxx')
  5.  
  6. def main():
  7. person = Person()
  8. person.get_name
  9.  
  10. if __name__ == '__main__':
  11. main()

2、@property的set,deleter,get

  1. class Goods(object):
  2. @property
  3. def price(self):
  4. print('@property')
  5.  
  6. @price.setter
  7. def price(self,value):
  8. print('@price.setter:'+str(value))
  9.  
  10. @price.deleter
  11. def price(self):
  12. print('@price.deleter')
  13.  
  14. obj = Goods()
  15. obj.price = 50
  16. obj.price
  17. del obj.price

3、@property demo

  1. class Goods(object):
  2. def __init__(self):
  3. #原价
  4. self.original_price = 100
  5. #折扣
  6. self.discount = 0.8
  7.  
  8. @property
  9. def price(self):
  10. #实际价格=原价*折扣
  11. new_price = self.original_price*self.discount
  12. return new_price
  13. @price.setter
  14. def price(self,value):
  15. self.original_price = value
  16. @price.deleter
  17. def price(self):
  18. del self.original_price
  19. obj = Goods()
  20. obj.price
  21. obj.price = 200
  22. del obj.price

4、property函数使用

  1. class Foo(object):
  2. def get_name(self):
  3. print('get_name')
  4. return 'laowang'
  5.  
  6. def set_name(self, value):
  7. '''必须两个参数'''
  8. print('set_name')
  9. return 'set value' + value
  10.  
  11. def del_name(self):
  12. print('del_name')
  13. return 'laowang'
  14.  
  15. NAME = property(get_name, set_name, del_name, 'description.')
  16.  
  17. obj = Foo()
  18. obj.NAME #调用get方法
  19. obj.NAME = 'alex' #调用set方法
  20. desc = Foo.NAME.__doc__ #调用第四个描述
  21. print(desc)
  22. del obj.NAME #调用第三个删除方法

5、property函数操作私有属性的get和set方法

  1. class Person(object):
  2. def __init__(self, age):
  3. self.__age = age
  4.  
  5. def set_age(self, value):
  6. self.__age = value
  7.  
  8. def get_age(self):
  9. return self.__age
  10.  
  11. AGE = property(get_age, set_age)
  12.  
  13. person = Person(15)
  14. person.AGE = 20
  15. print(str(person.AGE))

python中@property和property函数使用的更多相关文章

  1. Python 函数式编程 & Python中的高阶函数map reduce filter 和sorted

    1. 函数式编程 1)概念 函数式编程是一种编程模型,他将计算机运算看做是数学中函数的计算,并且避免了状态以及变量的概念.wiki 我们知道,对象是面向对象的第一型,那么函数式编程也是一样,函数是函数 ...

  2. Python中的高阶函数与匿名函数

    Python中的高阶函数与匿名函数 高阶函数 高阶函数就是把函数当做参数传递的一种函数.其与C#中的委托有点相似,个人认为. def add(x,y,f): return f( x)+ f( y) p ...

  3. python中enumerate()函数用法

    python中enumerate()函数用法 先出一个题目:1.有一 list= [1, 2, 3, 4, 5, 6]  请打印输出:0, 1 1, 2 2, 3 3, 4 4, 5 5, 6 打印输 ...

  4. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  5. Python中sort和sorted函数代码解析

    Python中sort和sorted函数代码解析 本文研究的主要是Python中sort和sorted函数的相关内容,具体如下. 一.sort函数 sort函数是序列的内部函数 函数原型: L.sor ...

  6. Python中进制转换函数的使用

    Python中进制转换函数的使用 关于Python中几个进制转换的函数使用方法,做一个简单的使用方法的介绍,我们常用的进制转换函数常用的就是int()(其他进制转换到十进制).bin()(十进制转换到 ...

  7. 第8.27节 Python中__getattribute__与property的fget、@property装饰器getter关系深入解析

    一. 引言 在<第7.23节 Python使用property函数定义属性简化属性访问的代码实现>和<第7.26节 Python中的@property装饰器定义属性访问方法gette ...

  8. Python中的内置函数

    2.1 Built-in Functions The Python interpreter has a number of functions built into it that are alway ...

  9. Python中str()与repr()函数的区别

    在 Python 中要将某一类型的变量或者常量转换为字符串对象通常有两种方法,即str()或者 repr() . >>> a = 10 >>> type(str(a ...

  10. python中os.path.isdir()函数的使用

    在python 中,os.path.isdir(path)函数主要用来判断函数内部的path是否为一个目录 具体关于这个函数的解说参考博客https://blog.csdn.net/xjp_xujip ...

随机推荐

  1. memcache分布式 存取

    Memcached分布式 Memcached虽然称为“分布式“缓存服务器,但服务器端并没有“分布式”的功能.Memcached的分布式完全是由客户端实现的.memcached是怎么实现分布式缓存的呢? ...

  2. 高可用api接口网络部署方案

    我们平时接触的产品都是7*24小时不间断服务,产品中的api接口肯定也是高可用的,下面我向大家分享一下互联网公司api接口高可用的网络部署方案.  我们一般通过http://le.quwenzhe.c ...

  3. ORACLE默认实例设置--linux

    数据库实例多了之后,每次export的时候,显示的ORACLE_SID总不是自己经常用的那个,要是能让默认的自定义就好了. 现在就解释一下在linux环境中如何定义: 1.su - oracle 2. ...

  4. python第十一课——转换结构

    3.转换函数:int():float():str():list():tuple():set():dict():bool(): 案例: #演示各个转换函数的使用: 数值型-->字符型使用:str( ...

  5. CF993E:Nikita and Order Statistics(FFT)

    Description 给你一个数组 $a_{1 \sim n}$,对于 $k = 0 \sim n$,求出有多少个数组上的区间满足:区间内恰好有 $k$ 个数比 $x$ 小.$x$ 为一个给定的数. ...

  6. Day4 数组

    双重for循环 外循环控制行,内循环控制列. //乘法表 ; i <= ; i++) { ; j <= i ;j++) { System.out.print(j+"*" ...

  7. 最简单的php学习

    php文件操作函数 filetype()判断文件的基本类型 egg 目录文件  文件  等  dir文件夹 file 文件 stat()函数获得指定文件名参数目标文件的基本属性 在php中以is_开头 ...

  8. DOS命令行简单用法

    DOS命令行简单用法 基本用法 1.cd(回车)从当前目录切回到根目录. 2.dir(回车)列出当前目录下的所有文件. 3.md kkk(回车)在当前目录下创建一个名称为kkk的文件夹. 4.rd k ...

  9. Spring源码分析(十二)FactoryBean的使用

    摘要:本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 一般情况下,Spring通过反射机制利用bean的class属性指定实现 ...

  10. dpkg安装失败解决过程

      终于好了.搞到转钟3点都没搞定,耽误不少时间. 执行sudo port install dpkg 报错如下Error: org.macports.build for port gmp return ...