当我执行以下操作时:
def Welcome(email, temporaryPassword):
model = object()
model.TemporaryPassword = temporaryPassword
model.Email = email
我收到一个错误:
AttributeError: 'object' object has no attribute 'TemporaryPassword'
如何像我正在动态地创建对象?
最佳答案
使用lambda对象:(http://codepad.org/ITFhNrGi)
def Welcome(email, temporaryPassword):
model = type('lamdbaobject', (object,), {})()
model.TemporaryPassword = temporaryPassword
model.Email = email
return model
t = Welcome('[email protected]','1234')
print(str(t.TemporaryPassword))
关于python - 使用Python在运行时动态地向类添加属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7383505/