12
Tôi đã tự hỏi liệu bạn có thể thêm một attibute vào một từ điển python hay không.Thêm một thuộc tính vào một từ điển python từ thư viện chuẩn
class myclass():
def __init__():
self.mydict = {} #initialize a regular dict
self.mydict.newattribute = "A description of what this dictionary will hold"
>>>AttributeError: 'dict' object has no attribute 'newattribute'
setattr(self.mydict,"attribute","A description of what this dictionary will hold"
>>>AttributeError: 'dict' object has no attribute 'newattribute'
Có cách nào để nhanh chóng thêm thuộc tính mô tả của tôi mà không phải sao chép lớp dict và quá tải hàm tạo. Tôi nghĩ nó sẽ đơn giản, nhưng tôi đoán tôi đã sai.
Cảm ơn, J