2014-02-05 68 views

回答

3

當您有globals(),getattr()setattr()時,請勿使用eval()

cls = globals()[someClass] 
instance = cls() 
for f in cls._meta.fields: 
    previous_value = getattr(instance, f) 
    setattr(instance, f, previous_value + 'some new value') 

我避免使用object作爲變量名稱;它會掩蓋內置的object() type