1
我试图编写一些代码来检查一个项目是否具有某些属性并调用它们。我试图用getattr来做到这一点,但修改不会是永久性的。我做了一个“虚拟”课来检查这个。 这里是我用于这个类的代码:使用getattr调用实例的方法[python]
class X:
def __init__(self):
self.value = 90
def __get(self):
return self.value
def __set(self,value):
self.value = value
value = property(__get,__set)
x = X()
print x.value # this would output 90
getattr(x,"value=",99) # when called from an interactive python interpreter this would output 99
print x.value # this is still 90 (how could I make this be 99 ?)
谢谢!
这就是我一直在寻找的!谢谢 ! – Geo 2009-01-27 17:00:31
我是Python新手,在什么情况下你会采用这种方法? – Kev 2009-01-27 17:04:20