我必须编写一个测试模块并具有 c++ 背景。也就是说,我知道 python 中没有指针,但是我如何实现以下目标:
我有一个测试方法,其伪代码如下所示:
def check(self,obj,prop,value):
if obj.prop <> value: #this does not work,
#getattr does not work either, (objects has no such method (interpreter output)
#I am working with objects from InCyte's python interface
#the supplied findProp method does not do either (i get
#None for objects I can access on the shell with obj.prop
#and yes I supply the method with a string 'prop'
if self._autoadjust:
print("Adjusting prop from x to y")
obj.prop = value #setattr does not work, see above
else:
print("Warning Value != expected value for obj")
Run Code Online (Sandbox Code Playgroud)
由于我想在不同的函数中检查许多不同的对象,因此我希望能够保留 check 方法。
一般来说,如何确保函数影响传递的对象并且不会创建副本?
myobj.size=5
resize(myobj,10)
print myobj.size #jython =python2.5 => print is not a function
Run Code Online (Sandbox Code Playgroud)
我无法调整成员方法的大小,因为实现myobj无法实现,而且我不想myobj=resize(myobj, 10)到处输入
另外,如何才能访问传递对象和属性名称的函数中的这些属性?
getattr 不是一个方法,你需要这样调用它
getattr(obj, prop)
Run Code Online (Sandbox Code Playgroud)
类似地 setattr 被这样调用
setattr(obj, prop, value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
236 次 |
| 最近记录: |