[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
...全文
362 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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)

37,743

社区成员

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

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