'MyClass' object has no attribute '__getitem__'
If you actually wanted to be able to access your attributes using inst["attr"]
and to explain your error, you would need to add a __getitem__
to you class:
class MyClass(object):
def __init__(self, id, a, b, c):
self.myList = []
self.id = id
self.a = a
self.b = b
self.c = c
def addData(self, data):
self.myList.append(data)
def __getitem__(self, item):
return getattr(self, item)
As others have noted, you can simply use
item.id
However, sometimes you do need to use this syntax if you are accessing a field dynamically:
item[dynamicField]
In that case, you can use the __getitem__() syntax as Anand suggested, however it is safer to use python's wrapper for __getitem__:
getattr(item, dynamicField)
item
is not a dictionary but a class so it has different syntax for accessing members. Access id
this way instead:
item.id