我有一個Python類這樣分配值一類動態
class Car(Base):
def __init__(self, details):
for field,value in details.iteritems():
self[field] = value
,我這樣稱呼它
car_details['make'] = 'Dodge'
car_details['model'] = 'Ram'
car = Car(car_details)
但是它失敗
TypeError: 'Car' object does not support item assignment
我想設置它,所以一旦類已經初始化,我就可以使用car.make
。
標題misleadi NG。你正試圖使用一個類實例作爲字典。 – delnan