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. 用windows自带的fsutil修改稀疏文件大小成功,但文件内容似乎丢失

    fsutil sparse setflag. fsutil sparse setrange 10M对应字节,1G对应字节.. 看文件属性,实际尺寸是小了,但内容似乎也丢了..因为自己把该文件做成虚拟盘 ...

  2. 在CentOS7上部署 Kubernetes集群

    yum -y install  etcd docker  flannel kubenetes 一般会遇到没有k8s源的问题,先 yum update -y 看是否有效,如果还是没用就创建yum 源,再 ...

  3. [IDE123] Intellij Idea 快捷键

    Ctrl+Shift+N,可以快速打开文件 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  4. ajax跨域调用webservice例子

    [WebMethod(Description = "这是一个描述")] public void GetTIM() { try { SqlDataAdapter da = new S ...

  5. hive遇到的问题以及解决办法

    hive java.lang.ClassNotFoundException: Class org.apache.hive.hcatalog.data.JsonSerDe not found hadoo ...

  6. windows内核Api的学习

    windows内核api就是ntoskrnl.exe导出的函数.我们能够跟调用应用层的api一样,调用内核api. 只是内核api须要注意的是.假设函数导出了.而且函数文档化(也就是能够直接在msdn ...

  7. 【jq】插件—弹出层layer.js

    layer.js包含了所有的层级情形,并且附加的有:tab层,相册层.webIM层. 适用于移动版本的layer.js   为layer for mobile 配套的layui 非常适合用于后台系统的 ...

  8. block本质探寻一之内存结构

    一.代码——命令行模式 //main.m #import <Foundation/Foundation.h> struct __block_impl { void *isa; int Fl ...

  9. ORM增删改查(django)

    单表操作: 一.添加 (1)方式一 from  mysite.models  import * def  add(request): book= Book(name="python" ...

  10. 【js】走近小程序(2) 常见问题总结

    一.API请求? 二.基础库兼容? 三.不同页面之间的传值   一.API请求? wx.request({ url: 'test.php', // 仅为示例,并非真实的接口地址 data: { x: ...