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. 2018 徐州赛区网赛 G. Trace

    题目链接在这里 题意是:按时间先后有许多左下角固定为(0,0),右上角为(xi,yi)的矩形浪潮,每次浪潮会留下痕迹,但是后来的浪潮又会冲刷掉自己区域的老痕,留下新痕迹,问最后留下的痕迹长度为多少? ...

  2. 【0】如何在电脑中使用多个python版本【python虚拟环境配置】

    问题: 该篇解决如何在同一个操作系统中可以便捷诶的使用多个python版本.有时候我们在开发的时候会同时需要python2 和python3环境,或者是需要不同的版本,都可以尽心如下配置. (1)在c ...

  3. 4-urllib库添加代理,添加请求头格式 模板

    urllib 库设置代理的方法 案例如下:

  4. python的unittest框架中如何删除测试数据,清理环境,可以通过addCleanup函数

    def addCleanup(self, function, *args, **kwargs): """Add a function, with arguments, t ...

  5. js实现svg图形转存为图片下载[转]

    我们知道canvas画布可以很方便的js原生支持转为图片格式并下载,但是svg矢量图形则并没有这方面原生的支持.研究过HighChart的svg图形的图片下载机制,其实现原理大体是浏览器端收集SVG代 ...

  6. BigDecimalUtil 工具类

    一.为什么要用BigDecimal? 涉及到加减乘除,用int,double 会出现数据丢失,这个时候就要用BigDecimal. 注意:在new BigDecimal(Double.toString ...

  7. regex_replace

    Regex_iterator方法需要输入一个正则表达式,以及一个用于替换匹配的字符串的格式化字符串:这个格式化的字符串可以通过表的转义序列引用匹配子字符串的部分内容: 转义序列 $n 替换第n个捕获的 ...

  8. 【转】Spring Boot干货系列:(六)静态资源和拦截器处理

    前言 本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类WebMvcConfigurerAdapter. 正文 前面章节我们也有简单介绍过SpringBoot中对静态资源的默认支持 ...

  9. JDBC通过配置文件(properites)读取数据库配置信息

    扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...

  10. Debian 8 安装录屏软件kazam

    1 安装 $ sudo apt-get install kazam [sudo] password for z: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完 ...