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. Django 通过APNS推送消息

    最近手上一个项目需要通过APNS向app推送消息,由于后端采用drf框架,在github上找了好多模块,最终发现pzanitti大神的推送模块 django-push-notifications 比较 ...

  2. Leuze BCL308i 使用方法整理

    1 硬件连接关系 1.1 接口盒 BCL308i一般选配MK308/MK348/MK358系列接口盒, 单独使用(不组成扫描集群)时需要连接3根线,分别为SERVICE .SW/PWR.HOST/BU ...

  3. 对于开发WEB方面项目需要的工具和技术了解

    1.IDE:Webstorm,JavaScript 开发工具. 2.版本管理系统:Git,独一无二. 3.单元测试:jsamine,前后端共用.Jasmine是我们梦寐以求的Javascript测试框 ...

  4. 【转】合格PHP工程师的知识结构

    PHP的运行环境 连环境都搞不起来,就是你有多么喜欢PHP,那也是白搭,开始我们大多会使用集成环境软件例如xampp,wamp.随着知识的增加慢慢要学会自己搭建运行环境,例如 Linux(Ubuntu ...

  5. Day14 集合(一)

    集合总体介绍 Java集合是java提供的工具包,包含了常用的数据结构:集合.链表.队列.栈.数组.映射等.Java集合工具包位置是java.util.*Java集合主要可以划分为4个部分:List列 ...

  6. print输出

    python中的print输出可以用逗号"," >>> a = 1 >>> b = 2 >>> print a,b 1 2 逗 ...

  7. PAT02-线性结构3 Reversing Linked List

    题目:https://pintia.cn/problem-sets/1010070491934568448/problems/1037889290772254722 先是看了牛客(https://ww ...

  8. 优化升级logging封装RotatingFileHandler

    1.升级优化,提供用户自定义日志level文件夹生成控制,提供日志错误显示到日志打印异常补获到日志 # coding=utf-8 import logging import time import o ...

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

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

  10. BUAA OO 2019 第一单元作业总结

    目录 总 架构 Controller​ Model​ 输入处理 代码静态分析 行数 方法复杂度 UML​ 类图 优点 缺点 坑 输入 非法的空白字符 输入的简并处理 运算 浅拷贝 可变类型与不可变类型 ...