Can someone explain what is happening here?
class Test(object):
__getitem__ = getattr
t = Test()
t['foo']
gives error (in Python 2.7 and 3.1):
TypeError: getattr expected at least 2 arguments, got 1
whereas:
def f(*params):
print params # or print(params) in 3.1
class Test(object):
__getitem__ = f
prints the two parameters I’d expect.
Confusingly, built-in functions (and certain other types of callables) do not become bound methods as normal functions do when used in a class:
Compared to:
So, getattr is not correctly receiving the first (‘self’) parameter. You’ll need to write a normal method to wrap it.