Pythonで,定義されたクラスにメソッドを追加する

import types
 
class A(object):
  def __init__(self, l):
    self.list = l
 
def foo(self, i):
  return self.list[i]
 
# print A.__getitem__ ## => AttributeError: type object 'A' has no attribute '__getitem__'
 
A.__getitem__ = types.MethodType(foo, None, A)
## or
# setattr(A, "__getitem__", types.MethodType(foo, None, A))
 
print A.__getitem__
 
a = A([1,2,3])
print a[0], a[1], a[2]
 
b = A([4,5,6,7])
print b[0], b[1], b[2], b[3]