[D]python编程property()用法

错_对 2012-07-01 04:19:49
python 2.5版本:


#这个版本无法正确运行
__metaclass__ = type

class rectangle :

def __init__(self, arg_width=0, arg_height=0) :
self.width = arg_width
self.height = arg_height

def setItem( self, *value ) : #可接受元祖参数值
self.width, self.height = value

def getItem(self) :
return self.width, self.height

def delItem(self) :
del self.width
del self.height

size = property(getItem,setItem,delItem, "display rectangle property")

#这个版本可以通过size返回属性,但不能通过size设置属性:
>>> rect = rectangle()
>>> rect.width , rect.height = 10, 20
>>> rect.size
(10, 20)
>>> rect.size(100,200) #通过size设置width ,height时

Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
rect.size(100,200)
TypeError: 'tuple' object is not callable
>>> rect.size = 100, 200 #通过size设置width ,height时

Traceback (most recent call last):
File "<pyshell#66>", line 1, in <module>
rect.size = 100, 200
File "C:/Users/TOSHIBA/Desktop/TmpFile/PythonCode/property", line 10, in setItem
self.width, self.height = value
ValueError: need more than 1 value to unpack
>>> rect.size = (100,200) #通过size设置width ,height时

Traceback (most recent call last):
File "<pyshell#67>", line 1, in <module>
rect.size = (100,200)
File "C:/Users/TOSHIBA/Desktop/TmpFile/PythonCode/property", line 10, in setItem
self.width, self.height = value
ValueError: need more than 1 value to unpack





#这个版本可以正确运行

__metaclass__ = type

class rectangle :

def __init__(self, arg_width=0, arg_height=0) :
self.width = arg_width
self.height = arg_height

def setItem( self, tuple ) : #显示定义可接受元祖参数
self.width, self.height = tuple

def getItem(self) :
return self.width, self.height

def delItem(self) :
del self.width
del self.height

size = property(getItem,setItem,delItem, "display rectangle property")




#这个是《python基础教程》(第2版) 上148页的例题,也无法正确运行
__metaclass__ = type

class rectangle :

def __init__(self) :
self.width = 0
self.height = 0

def setItem( self, value ) :
self.width, self.height = value

size = property(getItem,setItem)



官方说明:

property( [fget[, fset[, fdel[, doc]]]])

Return a property attribute for new-style classes (classes that derive from object).
fget is a function for getting an attribute value, likewise fset is a function for setting, and fdel a function for del'ing, an attribute. Typical use is to define a managed attribute x:



class C(object):
def __init__(self): self.__x = None
def getx(self): return self._x
def setx(self, value): self._x = value
def delx(self): del self._x
x = property(getx, setx, delx, "I'm the 'x' property.")



If given, doc will be the docstring of the property attribute. Otherwise, the property will copy fget's docstring (if it exists). This makes it possible to create read-only properties easily using property() as a decorator:


class Parrot(object):
def __init__(self):
self._voltage = 100000

@property
def voltage(self):
"""Get the current voltage."""
return self._voltage


turns the voltage() method into a ``getter'' for a read-only attribute with the same name.

New in version 2.2. Changed in version 2.5: Use fget's docstring if no doc given.


谁给讲解下。thx

---------------------------
Double行动:
原帖分数:20
帖子加分:20
...全文
358 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
m4vsak123 2013-10-11
  • 打赏
  • 举报
回复
有一点值得提醒: 使用property的类必须继承自object,否则不会正常工作的。
panghuhu250 2012-07-01
  • 打赏
  • 举报
回复
这和property没有关系,是关于可变参数的问题。在下面的函数中打印出value的值就清楚了。

    def setItem( self, *value ) :    #可接受元祖参数值
self.width, self.height = value


当执行rect.size=(100,200),它变成rect.setItem((100,200)), 因为value的值是所有的变量(self除外)组成的tuple,即((100,200),),是一个有一个元素的tuple,所以没法unpack为两个值。

还有就是即使是执行

rect.size = 100, 200
时,python也是要先把右侧的值pack起来,形成一个tuple,所以它等同于

rect.size = (100,200)
E:. │ 1.txt │ ├─千锋Python教程:第01章 第一个Python程序与数据存储及数据类型(9集) │ │ .DS_Store │ │ │ ├─code │ │ 1、数据存储.txt │ │ 2、第一个python程序.py │ │ 3、注释.py │ │ 4、输出与输入.py │ │ 5、Python数据类型.py │ │ 6、标识符.py │ │ 7、变量与常量.py │ │ │ ├─file │ │ │ MindManager_64bit_17.2.208.exe │ │ │ Python安装.pdf │ │ │ Python概述.pdf │ │ │ submit 2.0.rar │ │ │ │ │ ├─pycharm专业版 │ │ │ pycharm-professional-2017.2.3.exe │ │ │ Pycharm.txt │ │ │ │ │ └─python3.6 │ │ └─windows │ │ python-3.6.0-amd64.exe │ │ │ └─video │ 千锋Python教程:01.python概述和工具的安装.mp4 │ 千锋Python教程:02.数据存储与二进制操作1.mp4 │ 千锋Python教程:03.数据存储与二进制操作2.mp4 │ 千锋Python教程:04.第一个Python程序与注释及输入输出.mp4 │ 千锋Python教程:05.Python数据类型,标识符,变量与常量以及Number数据类型1.mp4 │ 千锋Python教程:06.Python数据类型,标识符,变量与常量以及Number数据类型2.mp4 │ 千锋Python教程:07.Python数据类型,标识符,变量与常量以及Number数据类型3.mp4 │ 千锋Python教程:08.数学功能与数字类型转换的使用1.mp4 │ 千锋Python教程:09.数学功能与数字类型转换的使用2.mp4 │ ├─千锋Python教程:第02章 运算符与表达式(7集) │ │ .DS_Store │ │ │ ├─code │ │ 1、运算符与表达式.py │ │ 2、运算符与表达式.py │ │ │ └─video │ 千锋Python教程:10.算术&赋值&位&关系运算符与表达式1.mp4 │ 千锋Python教程:11.算术&赋值&位&关系运算符与表达式2.mp4 │ 千锋Python教程:12.逻辑运算符与表达式1.mp4 │ 千锋Python教程:13.逻辑运算符与表达式2.mp4 │ 千锋Python教程:14.成员&身份运算符&字符串1.mp4 │ 千锋Python教程:15.成员&身份运算符&字符串2.mp4 │ 千锋Python教程:16.成员&身份运算符&字符串3.mp4 │ ├─千锋Python教程:第03章 字符串&布尔&空值(7集) │ │ .DS_Store │ │ │ ├─code │ │ 1、String(字符串).py │ │ 2、String的内置函数.py │ │ 3、布尔值和空值.py │ │ 4、变量的类型问题.py │ │ │ └─video │ 千锋Python教程:17.运算符&字符串1.mp4 │ 千锋Python教程:18.运算符&字符串2.mp4 │ 千锋Python教程:19.字符串的使用1.mp4 │ 千锋Python教程:20.字符串的使用2.mp4 │ 千锋Python教程:21.字符串的使用3.mp4 │ 千锋Python教程:22.字符串&布尔值&空值&变量的类型问题1.mp4 │ 千锋Python教程:23.字符串&布尔值&空值&变量的类型问题2.mp4 │ ├─千锋Python教程:第04章 列表&元组&流程控制语句(8集) │ │ .DS_Store │ │ │ ├─code │ │ 1、list(列表).py │ │ 2、列表方法.py │ │ 3、浅拷贝与深拷贝.py │ │ 4、tuple(元组).py │ │ 5、条件控制语句.py │ │ 6、循环语句(while).py │ │ 7、循环语句(for).py │ │ 8、pass语句&continue;语句与break语句.py │ │ │ └─video │ 千锋Python教程:24.列表的使用及深浅拷贝1.mp4 │ 千锋Python教程:25.列表的使用及深浅拷贝2.mp4 │ 千锋Python教程:26.列表的使用及深浅拷贝3.mp4 │ 千锋Python教程:27.深浅拷贝&元组&条件判断语句1.mp4 │ 千锋Python教程:28.深浅拷贝&元组&条件判断语句2.mp4 │ 千锋Python教程:29.循环语句&关键字 break&pass;&continue1;.mp4 │ 千锋Python教程:30.循环语句&关键字 break&pass;&continue2;.mp4 │ 千锋Python教程:31.循环语句&关键字 break&pass;&continue3;.mp4 │ ├─千锋Python教程:第05章 字典&集合&类型转换&turtle;(1集) │ │ .DS_Store │ │ │ ├─code │ │ 1、dict(字典).py │ │ 2、set.py │ │ 3、类型转换.py │ │ │ └─video │ 千锋Python教程:32.字典&集合&类型转换&turtle1;.mp4 │ ├─千锋Python教程:第06章 函数与高阶函数(7集)) │ │ .DS_Store │ │ │ ├─code │ │ 10、函数也是一种数据.py │ │ 11、匿名函数.py │ │ 12、map&reduce;.py │ │ 13、filter.py │ │ 14、sorted.py │ │ 15、作用域.py │ │ 16、体现作用域.py │ │ 17、修改全局变量.py │ │ 18、修改嵌套作用域中的变量.py │ │ 1、函数概述.py │ │ 2、最简单的函数(无参无返回值).py │ │ 3、函数的参数.py │ │ 4、函数的返回值.py │ │ 5、传递参数.py │ │ 6、关键字参数.py │ │ 7、默认参数.py │ │ 8、不定长参数.py │ │ 9、多个返回值.py │ │ │ └─video │ 千锋Python教程:33.函数概述.mp4 │ 千锋Python教程:34.函数的基本使用1.mp4 │ 千锋Python教程:35.函数的基本使用2.mp4 │ 千锋Python教程:36.匿名函数&高阶函数 map&reduce1;.mp4 │ 千锋Python教程:37.匿名函数&高阶函数 map&reduce2;.mp4 │ 千锋Python教程:38.高阶函数 filter&sorted;.mp4 │ 千锋Python教程:39.作用域&修改变量作用域.mp4 │ ├─千锋Python教程:第07章 闭包&装饰器(5集) │ │ .DS_Store │ │ │ ├─code │ │ 10、多个装饰器.py │ │ 11、装饰器使用场景.py │ │ 12、计数函数执行次数.py │ │ 13、retry装饰器.py │ │ 1、变量的作用域链.py │ │ 2、利用闭包突破作用域链.py │ │ 3、装饰器概念.py │ │ 4、简单装饰器.py │ │ 5、复杂装饰器.py │ │ 6、使用@符号装饰.py │ │ 7、通用装饰器.py │ │ 8、参数的装饰器.py │ │ 9、计算程序运行时间.py │ │ │ └─video │ 千锋Python教程:40.闭包&装饰器1.mp4 │ 千锋Python教程:41.闭包&装饰器2.mp4 │ 千锋Python教程:42.闭包&装饰器3.mp4 │ 千锋Python教程:43.装饰器的使用1.mp4 │ 千锋Python教程:44.装饰器的使用2.mp4 │ ├─千锋Python教程:第08章 迭代器&生成器&偏函数(6集) │ 千锋Python教程:45.可迭代对象&列表生成式&生成器1.mp4 │ 千锋Python教程:46.可迭代对象&列表生成式&生成器2.mp4 │ 千锋Python教程:47.可迭代对象&列表生成式&生成器3.mp4 │ 千锋Python教程:48.斐波拉契数列&迭代器.mp4 │ 千锋Python教程:49.杨辉三角&偏函数&模块概述1.mp4 │ 千锋Python教程:50.杨辉三角&偏函数&模块概述2.mp4 │ ├─千锋Python教程:第09章 模块&包&常用模块&三方模块(14集) │ 千锋Python教程:51.系统模块&自定义模块&包1.mp4 │ 千锋Python教程:52.系统模块&自定义模块&包2.mp4 │ 千锋Python教程:53.系统模块&自定义模块&包3.mp4 │ 千锋Python教程:54.time 模块1.mp4 │ 千锋Python教程:55.time 模块2.mp4 │ 千锋Python教程:56.datetime&calendar;&collections1;.mp4 │ 千锋Python教程:57.datetime&calendar;&collections2;.mp4 │ 千锋Python教程:58.collections&uuid;&base64;模块1.mp4 │ 千锋Python教程:59.collections&uuid;&base64;模块2.mp4 │ 千锋Python教程:60.collections&uuid;&base64;模块3.mp4 │ 千锋Python教程:61.base64&hashlib;&hmac;模块1.mp4 │ 千锋Python教程:62.base64&hashlib;&hmac;模块2.mp4 │ 千锋Python教程:63.itertools 模块&三方模块的安装&pillow; 模块1.mp4 │ 千锋Python教程:64.itertools 模块&三方模块的安装&pillow; 模块2.mp4 │ ├─千锋Python教程:第10章 面向对象(26集) │ 千锋Python教程:65.堆和栈&面向对象思想概述1.mp4 │ 千锋Python教程:66.堆和栈&面向对象思想概述2.mp4 │ 千锋Python教程:67.堆和栈&面向对象思想概述3.mp4 │ 千锋Python教程:68.创建类&对象&对象的方法1.mp4 │ 千锋Python教程:69.创建类&对象&对象的方法2.mp4 │ 千锋Python教程:70.类属性&对象属性&构造方法&析构方法&访问权限1.mp4 │ 千锋Python教程:71.类属性&对象属性&构造方法&析构方法&访问权限2.mp4 │ 千锋Python教程:72.类属性&对象属性&构造方法&析构方法&访问权限3.mp4 │ 千锋Python教程:73.@property 装饰器&__slots__限制&单例概述1.mp4 │ 千锋Python教程:74.@property 装饰器&__slots__限制&单例概述2.mp4 │ 千锋Python教程:75.单例的三种实现方式&__repr__&__str__&继承概述1.mp4 │ 千锋Python教程:76.单例的三种实现方式&__repr__&__str__&继承概述2.mp4 │ 千锋Python教程:77.继承的实现&继承体系&栈和队列&python2;.2之前的继承体系1.mp4 │ 千锋Python教程:78.继承的实现&继承体系&栈和队列&python2;.2之前的继承体系2.mp4 │ 千锋Python教程:79.继承的实现&继承体系&栈和队列&python2;.2之前的继承体系3.mp4 │ 千锋Python教程:80.两种继承体系的区别.mp4 │ 千锋Python教程:81.python2.3-2.7的集成体系&py3;的继承体系&多态1.mp4 │ 千锋Python教程:82.python2.3-2.7的集成体系&py3;的继承体系&多态2.mp4 │ 千锋Python教程:83.Mixin&运算符重载&属性监听&枚举类1.mp4 │ 千锋Python教程:84.Mixin&运算符重载&属性监听&枚举类2.mp4 │ 千锋Python教程:85.Mixin&运算符重载&属性监听&枚举类3.mp4 │ 千锋Python教程:86.垃圾回收机制&类装饰器&魔术方法&人射击子弹案例1.mp4 │ 千锋Python教程:87.垃圾回收机制&类装饰器&魔术方法&人射击子弹案例2.mp4 │ 千锋Python教程:88.垃圾回收机制&类装饰器&魔术方法&人射击子弹案例3.mp4 │ 千锋Python教程:89.邮件&短信发送1.mp4 │ 千锋Python教程:90.邮件&短信发送2.mp4 │ ├─千锋Python教程:第11章 银行操作系统&tkinter; 界面(14集) │ 千锋Python教程:100.Entry控件&其他控件使用演示1.mp4 │ 千锋Python教程:101.Entry控件&其他控件使用演示2.mp4 │ 千锋Python教程:102.其他控件使用演示.mp4 │ 千锋Python教程:103.其他控件使用演示1.mp4 │ 千锋Python教程:104.其他控件使用演示2.mp4 │ 千锋Python教程:91.贪吃蛇演示&银行操作系统1.mp4 │ 千锋Python教程:92.贪吃蛇演示&银行操作系统2.mp4 │ 千锋Python教程:93.贪吃蛇演示&银行操作系统3.mp4 │ 千锋Python教程:94.银行操作系统.mp4 │ 千锋Python教程:95.银行操作系统1.mp4 │ 千锋Python教程:96.银行操作系统2.mp4 │ 千锋Python教程:97.银行操作系统&GUI;概述&tkinter; 概述1.mp4 │ 千锋Python教程:98.银行操作系统&GUI;概述&tkinter; 概述2.mp4 │ 千锋Python教程:99.tkinter组件之 label&button;.mp4 │ ├─千锋Python教程:第12章 异常处理&代码调试&IO;编程&目录遍历(14集) │ 千锋Python教程:105.错误处理1.mp4 │ 千锋Python教程:106.错误处理2.mp4 │ 千锋Python教程:107.代码调试1.mp4 │ 千锋Python教程:108.代码调试2.mp4 │ 千锋Python教程:109.单元测试1.mp4 │ 千锋Python教程:110.单元测试2.mp4 │ 千锋Python教程:111.树状目录层级演示&文档测试&读文件1.mp4 │ 千锋Python教程:112.树状目录层级演示&文档测试&读文件2.mp4 │ 千锋Python教程:113.写文件&编码与解码&StringIO;与B ytesIO1.mp4 │ 千锋Python教程:114.写文件&编码与解码&StringIO;与B ytesIO2.mp4 │ 千锋Python教程:115.os模块&数据持久化文件操作1.mp4 │ 千锋Python教程:116.os模块&数据持久化文件操作2.mp4 │ 千锋Python教程:117.目录遍历1.mp4 │ 千锋Python教程:118.目录遍历2.mp4 │ ├─千锋Python教程:第13章 正则表达式(5集) │ 千锋Python教程:119.正则表达式概述&re; 模块概述&常用函数&单字符匹配语法1.mp4 │ 千锋Python教程:120.正则表达式概述&re; 模块概述&常用函数&单字符匹配语法2.mp4 │ 千锋Python教程:121.正则表达式概述&re; 模块概述&常用函数&单字符匹配语法3.mp4 │ 千锋Python教程:122.正则表达式深入方式使用1.mp4 │ 千锋Python教程:123.正则表达式深入方式使用2.mp4 │ ├─千锋Python教程:第14章 进程和线程(12集) │ 千锋Python教程:124.多任务原理&进程概述&单任务现象&实现多任务1.mp4 │ 千锋Python教程:125.多任务原理&进程概述&单任务现象&实现多任务2.mp4 │ 千锋Python教程:126.多任务原理&进程概述&单任务现象&实现多任务3.mp4 │ 千锋Python教程:127.父子进程&启动进程&进程对象封装1.mp4 │ 千锋Python教程:128.父子进程&启动进程&进程对象封装2.mp4 │ 千锋Python教程:129.进程间的通信&线程概述&启动多线程1.mp4 │ 千锋Python教程:130.进程间的通信&线程概述&启动多线程2.mp4 │ 千锋Python教程:131.线程间数据共享&线程锁1.mp4 │ 千锋Python教程:132.线程间数据共享&线程锁2.mp4 │ 千锋Python教程:133.线程间数据共享&线程锁3.mp4 │ 千锋Python教程:134.定时线程&线程通信&生产者与消费者&线程调度1.mp4 │ 千锋Python教程:135.定时线程&线程通信&生产者与消费者&线程调度2.mp4 │ ├─千锋Python教程:第15章 网络编程(6集) │ 千锋Python教程:136.网络编程概述1.mp4 │ 千锋Python教程:137.网络编程概述2.mp4 │ 千锋Python教程:138.基于TCP的网络编程1.mp4 │ 千锋Python教程:139.基于TCP的网络编程2.mp4 │ 千锋Python教程:140.基于UDP的网络编程.mp4 │ 千锋Python教程:141.全网轰炸.mp4 │ ├─千锋Python教程:第16章 协程&同步异步&并发并行&编码(11集)规范 │ 千锋Python教程:142.协程概述&数据传递&生产者与消费者1.mp4 │ 千锋Python教程:143.协程概述&数据传递&生产者与消费者2.mp4 │ 千锋Python教程:144.同步异步&asyncio;模块块&协程与任务的定义及阻塞与 await1.mp4 │ 千锋Python教程:145.同步异步&asyncio;模块块&协程与任务的定义及阻塞与 await2.mp4 │ 千锋Python教程:146.同步异步&asyncio;模块块&协程与任务的定义及阻塞与 await3.mp4 │ 千锋Python教程:147.并发并行&协程嵌套&获取网页数据1.mp4 │ 千锋Python教程:148.并发并行&协程嵌套&获取网页数据2.mp4 │ 千锋Python教程:149.并发并行&协程嵌套&获取网页数据3.mp4 │ 千锋Python教程:150.chardet 模块&py2;与py3的区别&PEP8;编码规范1.mp4 │ 千锋Python教程:151.chardet 模块&py2;与py3的区别&PEP8;编码规范2.mp4 │ 千锋Python教程:152.chardet 模块&py2;与py3的区别&PEP8;编码规范3.mp4 │ └─千锋Python教程:第17章 Linux&git;(23集) 千锋Python教程:153.Linux概述1.mp4 千锋Python教程:154.Linux概述2.mp4 千锋Python教程:155.git的使用1.mp4 千锋Python教程:156.git的使用2.mp4 千锋Python教程:157.git的使用3.mp4 千锋Python教程:158.git 的使用1.mp4 千锋Python教程:159.git 的使用2.mp4 千锋Python教程:160.安装虚拟机&Ubantu; 镜像1.mp4 千锋Python教程:161.安装虚拟机&Ubantu; 镜像2.mp4 千锋Python教程:162.安装虚拟机&Ubantu; 镜像3.mp4 千锋Python教程:163.Linux 命令1.mp4 千锋Python教程:164.Linux 命令2.mp4 千锋Python教程:165.linux 命令1.mp4 千锋Python教程:166.linux 命令2.mp4 千锋Python教程:167.linux 命令&远程连接 linux.mp4 千锋Python教程:168.vi 编辑器1.mp4 千锋Python教程:169.vi 编辑器2.mp4 千锋Python教程:170.用户管理权限&阿里云的使用1.mp4 千锋Python教程:171.用户管理权限&阿里云的使用2.mp4 千锋Python教程:172.手动安装 Python3.6的环境&虚拟机环境1.mp4 千锋Python教程:173.手动安装 Python3.6的环境&虚拟机环境2.mp4 千锋Python教程:174.git 的使用1.mp4 千锋Python教程:175.git 的使用2.mp4

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧