在 Javascript 中是:
var newObject = { 'propertyName' : 'propertyValue' };
newObject.propertyName; // returns "propertyValue"
但是 Python 中的相同语法会创建一个字典,这不是我想要的
new_object = {'propertyName': 'propertyValue'}
new_object.propertyName # raises an AttributeError
最佳答案
obj = type('obj', (object,), {'propertyName' : 'propertyValue'})
关于python - 如何创建具有属性的内联对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1528932/