Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我尝试为某些类的属性赋值,如下所示:
for name in dir(modelType): if request.get(name): getattr(model, name) = request.get(name)
但得到excption:“不能分配给函数调用”
如何在编译时不知道属性的情况下更改属性?
您用于setattr()为属性分配值。
setattr()
请参阅: http ://docs.python.org/library/functions.html#setattr
setattr(model, name, request.get(name))
但我建议将请求数据保存在字典、专用类中或直接访问它 - 除非您正在执行特定的元编程/内省,否则 setattr 通常是错误的工作工具。