[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
...全文
433 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)
内容概要:本文系统研究了Picard迭代法在非线性常微分方程参数估计中的应用,深入阐述了该方法的数学原理及其在参数辨识中的收敛性与稳定性优势。通过构建最小化误差的目标函数,并结合数值积分技术,采用迭代方式逐步逼近系统的真实参数值,有效解决了非线性动态系统中因缺乏解析解而难以进行精确建模的问题。文中提供了完整的Matlab代码实现,涵盖模型定义、迭代求解、参数更新与结果可视化等关键环节,增强了方法的可操作性与工程实用性。研究通过典型非线性系统案例验证了算法的有效性,展示了其在科学计算与工程建模中的良好适应性与推广潜力。; 适合人群:具备常微分方程理论、数值分析基础及Matlab编程能力,从事系统建模、参数辨识、动力学仿真等相关方向的研究生、科研人员和工程技术开发者。; 使用场景及目标:①解决实际工程中非线性微分方程模型的未知参数估计问题;②深入理解Picard迭代法在科学计算中的实现机制与数值特性;③为学术论文复现、科研项目开发或课程设计提供可运行、易调试的技术方案与代码参考。; 阅读建议:建议读者结合文中的数学推导与Matlab代码逐行分析,重点关注迭代流程、目标函数构造与数值积分的耦合实现,通过修改模型结构或噪声条件进行扩展实验,以深化对算法鲁棒性与适用边界的理解。配套资源可通过指定公众号和网盘链接获取,推荐同步学习以加速科研进程。
内容概要:本文详细介绍了一种基于多尺度集成极限学习机(Extreme Learning Machine, ELM)的回归方法,并提供了完整的Matlab代码实现。该方法通过构建多尺度特征表示与集成学习机制,有效提升了ELM在处理非线性、高维复杂数据时的预测精度与模型鲁棒性,特别适用于时间序列回归任务。文档不仅阐述了算法的核心原理与技术流程,还系统展示了其在风电功率预测等工程场景中的应用潜力。同时,文中附带了丰富的科研仿真案例集合,涵盖智能优化算法、深度学习、信号处理、电力系统调度等多个前沿方向,体现了多学科交叉融合的技术优势与实践价值。; 适合人群:具备一定Matlab编程能力,从事科学研究或工程应用的研究生、科研人员及工程技术开发者,尤其适合专注于机器学习、智能算法优化、新能源预测与电力系统建模等相关领域的专业人员。; 使用场景及目标:①用于风电、光伏、负荷等时间序列数据的高精度回归预测任务;②为科研工作者提供可复现的多尺度集成ELM模型代码框架,支持快速算法验证与二次开发;③满足实际工程项目中对高效建模、实时预测与智能决策的技术需求。; 阅读建议:建议读者结合所提供的Matlab代码进行动手实践,深入理解多尺度特征构造与集成策略的设计思想,同时可参考文档中其他相关算法案例进行横向比较与综合应用,以提升整体科研创新能力。

37,737

社区成员

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

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