我试图编写一些代码来检查项目是否具有某些属性,并调用它们。我试图用 getattr 做到这一点,但修改不会是永久性的。我做了一个“虚拟”类来检查这个。这是我用于课程的代码:
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 ? )
谢谢 !