12
Me preguntaba si podría agregar un atributo a un diccionario de Python.Agregar un atributo a un diccionario de Python de la biblioteca estándar
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'
De todas formas hay que añadir rápidamente mi atributo de descripción sin tener que copiar la clase dict y sobrecargar el constructor. Pensé que sería simple, pero creo que estaba equivocado.
Gracias, J