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. #001 WebStrom SVN使用技巧

    WebStrom中SVN 的一些使用技巧 2016-03-23 17:11:52 星期三 使用SVN的目录,是为了来管理代码的版本. 服务端语言 都有比较完善的IDE,前端JS代码,由于之前一直都用 ...

  2. 面向对象程序设计_课堂作业_01_Circle

    The 1st classwork of the C++ program 题目: Create a program that asks for the radius of a circle and p ...

  3. BZOJ4517:[SDOI2016]排列计数(组合数学,错排公式)

    Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值为 i,则称 i 是稳定的.序列恰好有 m 个数是 ...

  4. 1491. [NOI2007]社交网络【最短路计数】

    Description 在社交网络(socialnetwork)的研究中,我们常常使用图论概念去解释一些社会现象.不妨看这样的一个问题. 在一个社交圈子里有n个人,人与人之间有不同程度的关系.我们将这 ...

  5. Python全栈开发:list 、tuple以及dict的总结

    总结: 列表:增:append(),inset(),extend() 删:pop(),remove(),clear(),del 改:a.通过指定元素和切片重新赋值.b.可以使用repelace替换列表 ...

  6. 最新版的Chrome 69.0 设置始终开启flash而不是先询问

    ## 69.0 之前的版本 ##   1.打开 chrome://settings/content/flash   2.禁止网站运行Flash -> 改为“Ask (Default)”   3. ...

  7. Django输入 中文参数保存异常解决方法

    WEB页面输入中文后保存,出现异常     cmd.exe打印如下信息: UnicodeEncodeError: 'ascii' codec can't encode characters in po ...

  8. centos下Zabbix Agent端部署和安装

    首先重复一下前面的规划 server端:  192.168.136.144    centos6.5 (虚拟机) agent端:    192.168.136.155    centos6.5( 虚拟 ...

  9. x$ksppi与x$ksppcv查询隐藏参数

    数据库版本:oracle11g 11.0.2.0.4 SQL> desc x$ksppi; Name  Null?  Type -------------------- -------- --- ...

  10. block本质探寻八之循环引用

    说明:阅读本文,请参照之前的block文章加以理解: 一.循环引用的本质 //代码——ARC环境 void test1() { Person *per = [[Person alloc] init]; ...