property属性

  • 概念:

    • 定义一个方法但是使用装饰器property,只可以有一个self形参
    • 可以用这样的属性动态的获取属性的值
  • 定义方式(经典类)
 class Fun():
@property
def size(self):
return 100 fun = Fun()
print(fun.size)

100

  • 定义方式(新式类)
class Fun():
def __init__(self):
self.num = 0 @property
def price(self):
print("get @property")
return self.num @price.setter
def price(self, value):
self.num = value
print("set @price.setter") @price.deleter
def price(self):
print("del @price.deleter") obj = Fun()
print(obj.price) # 可以获取价格,调用property装饰器
obj.price = 100 # 修改价格,调用price.setter装饰器
del obj.price # 删除价格,调用price.deleter装饰器
get @property
0
set @price.setter
del @price.deleter`
  • 使用property方法来创建property属性

可以使用proerty来替代原有的封装的set和get方法

#coding=utf-8
class Foo(object):
def get_bar(self):
print("getter...")
return "laowang" def set_bar(self, value):
"""必须两个参数"""
print("setter...")
return 'set value' + value def del_bar(self):
print("deleter...")
return "laowang" BAR = property(get_bar, set_bar, del_bar, "description...") obj = Foo() obj.BAR # 自动调用第一个参数中定义的方法:get_bar
obj.BAR = "alex" # 自动调用第二个参数中定义的方法:set_bar方法,并将“alex”当作参数传入
desc = Foo.BAR.__doc__ # 自动获取第四个参数中设置的值:description...
print(desc)
del obj.BAR # 自动调用第三个参数中定义的方法:del_bar方法

Python中property属性的概论和使用方法的更多相关文章

  1. python 中 property 属性的讲解及应用

    Python中property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回 property属性的有两种方式: 1. 装饰器 即:在方法上应用装饰器 2. 类属性 即 ...

  2. python中property属性的介绍及其应用

    Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回. 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的 ...

  3. python 中property函数如何实现

    实际上,在python中property(fget,fset,fdel,doc)函数不是一个真正的函数,他其实是拥有很多特殊方法的类. 这特殊类总的很多方法完成了property函数中的所有工作,涉及 ...

  4. python中@property装饰器的使用

    目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...

  5. 【转】python之property属性

    1. 什么是property属性 一种用起来像是使用的实例属性一样的特殊属性,可以对应于某个方法 # ############### 定义 ############### class Foo: def ...

  6. OC中@property属性关键字的使用(assign/weak/strong/copy)

    OC中@property属性关键字的使用(assign/weak/strong/copy) 一.assign 用于 ‘基本数据类型’.‘枚举’.‘结构体’ 等非OC对象类型 eg:int.bool等 ...

  7. Python中的属性访问与描述符

    Python中的属性访问与描述符 请给作者点赞--> 原文链接 在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个nam ...

  8. Python中执行系统命令常见的几种方法--转载

    Python中执行系统命令常见的几种方法 Python中执行系统命令常见的几种方法有: (1)os.system # 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息 # 如果再命令行下执 ...

  9. Python中日期和时间格式化输出的方法

    本文转自:https://www.jb51.net/article/62518.htm 本文实例总结了python中日期和时间格式化输出的方法.分享给大家供大家参考.具体分析如下: python格式化 ...

随机推荐

  1. Java进阶学习(3)之对象容器(下)

    对象数组 对象数组中的每个元素都是对象的管理者而非对象本身 对象数组的for—each循环 集合容器(HashSet) HashSet 数学中的集合,元素间满足互异性.确定性.无序性 HashSet& ...

  2. 数制的运用-CodeForces - 535B

    题解: 因为每一位只可能是4或者7,可以类比二进制的思想. 基数为2,每一位的权值为2i-1:数字4表示的大小为1*2i-1:数字7表示的大小为2*2i-1. 将给定的n按照这种方法进行分解,求和.即 ...

  3. linux查看公网ip的方法

    curl ifconfig.me 或者 curl cip.cc

  4. Electron调用.Net的Dll以及将.Net程序作为子进程运行

    调用.Net Dll const edge = require('electron-edge-js'); var testDll = edge.func({ assemblyFile: ". ...

  5. Mysql常用的sql语句

    替换某字段的字符串: UPDATE article SET content = replace(content, '解决', '解放') WHERE ID<5000; 清空数据库,id也置空: ...

  6. Python引用某一文件的方法出现红色波浪线

    from parse import parse_url#引用parse里面的方法 结果出现波浪线并提示 This inspection detects names that should resolv ...

  7. JDBC 预编译语句对象

    Statement的安全问题:Statement的执行其实是直接拼接SQL语句,看成一个整体,然后再一起执行的. String sql = "xxx"; // ? 预先对SQL语句 ...

  8. 【PAT甲级】1066 Root of AVL Tree (25 分)(AVL树建树模板)

    题意: 输入一个正整数N(<=20),接着输入N个结点的值,依次插入一颗AVL树,输出最终根结点的值. AAAAAccepted code: #define HAVE_STRUCT_TIMESP ...

  9. mcast_join_source_group函数

    #include <errno.h> #include <net/if.h> #include <sys/socket.h> #define SA struct s ...

  10. Android的界面组件使用之ImageButton和ImageView,ImageSwitcher和GridView

    (一)ImageButton和ImageView ImageButton与Button的功能完全相同,只是ImageButton上显示的是图像,并且每个ImageButton组件都必须指定一个id,以 ...