Reputation: 2356
I have a class like this:
class MyClass:
def __init__(self, id, names):
self.id = id
self.names = names
Later on, I'm doing this:
classlist = []
classlist.append(MyClass("1", {"Key1", "Value"})) #add first key-value pair
classlist[0].names["Key2"] = "New Value" #add second key-value pair
but it fails on the third line with
TypeError: 'set' object does not support item assignment
.
I'm new to Python, please teach me how to do this correctly.
Upvotes: 2
Views: 171
Reputation: 132018
You are passing in a set, not a dictionary. It should be
classlist.append(MyClass("1", {"Key1": "Value"}))
Notice the :
instead of ,
separating the Key1 and Value. {arg, arg, arg}
is shorthand for creating a set.
Upvotes: 6