Answer the question
In order to leave comments, you need to log in
__getitem__ cannot be @classmethod
For example, this is the class:
class Class(object):
@classmethod
def getitem(*args):
print 'getitem %s' % (args,)
@classmethod
def __getitem__(*args):
print '__getitem__ %s' % (args,)
Class
when called both from the class and from the object calling Class.getitem(test) getitem(<class '__main__.Class'>, 'test') calling obj.getitem(test) getitem(<class '__main__.Class'>, 'test') calling Class[test] 'type' object has no attribute '__getitem__' calling obj[test] __getitem__(<class '__main__.Class'>, 'test')
Answer the question
In order to leave comments, you need to log in
while writing here on stackoverflow already answered:
All __magic__ methods are looked up in the dictionary of the class, not the object.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question