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函数使用的更多相关文章

  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. C++ new和delete重载

    C++ new和delete重载 2012-02-15 23:25:33|  分类: C/C++|举报|字号 订阅           首先,new和delete是运算符,重载new和delete是可 ...

  2. 占位符 %s

    a = input("Name:")b = input("Age:")c = input("Job:")d = input("ho ...

  3. Java编程练习题

      曾经,有人说过,没有刷题的人生是不完整的.看了几天Java,我试着做了几道练习题,好让我的人生完整一点.(偷笑--)这里挑了一些题来跟大家分享,本文不定期更新. 题目集  1. 最后一个单词的长度 ...

  4. 【笔记】JS数据类型总结

    JavaScript有六种数据类型,分别为undefined.null.number.string.Boolean.object,前面的五种是基础数据类型,也称之为原始类型,也就是无法再细分的基本类型 ...

  5. 【理解】 Error 10053和 Error 10054

    1. 10053 这个错误码的意思是:  A established connection was aborted by the software in your host machine, 一个已建 ...

  6. grep命令.md

    grep命令 简介 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Prin ...

  7. 《信息安全技术》实验二 Windows口令破解

    <信息安全技术>实验二 Windows口令破解 实验目的 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破解 实验环境 实验机Windows Server ...

  8. jenkins+pytest+ allure运行多个py文件测试用例

    jenkins的pytest运行多个py文件,导出allure报告方法,只需改下job的配置中的构建即可(pytest会运行指定文件下的所有test开头的py文件),如下:              ...

  9. cocoaPods 创建自己的依赖库

    1.先在github上创建一个仓库 和一般创建一样,就是证书一定要选,我选的是MIT,不要问我因为啥, 我也不知道, 哈哈 2.check到本地或者本地创建,反正最后都要上传到这个仓库,以个人习惯吧 ...

  10. P1120 小木棍 [数据加强版]

    题目描述 乔治有一些同样长的小木棍,他把这些木棍随意砍成几段,直到每段的长都不超过50. 现在,他想把小木棍拼接成原来的样子,但是却忘记了自己开始时有多少根木棍和它们的长度. 给出每段小木棍的长度,编 ...