code: def AddMethod(obj, function, name=None):
      
      if name is None:
          name = function.__name__
      else:
          function = RenameFunction(function, name)
  
      # Note the Python version checks - WLB
      # Python 3.3 dropped the 3rd parameter from types.MethodType
      if hasattr(obj, '__class__') and obj.__class__ is not type:
          # "obj" is an instance, so it gets a bound method.
          if sys.version_info[:2] > (3, 2):
              method = MethodType(function, obj)
          else:
              method = MethodType(function, obj, obj.__class__)
      else:
          # Handle classes
          method = function
  
      setattr(obj, name, method)
docs: Adds either a bound method to an instance or the function itself (or an unbound method in Python 2) to a class.
      If name is ommited the name of the specified function
      is used by default.
  
      Example::
  
          a = A()
          def f(self, x, y):
          self.z = x + y
          AddMethod(f, A, "add")
          a.add(2, 4)
          print(a.z)
          AddMethod(lambda self, i: self.l[i], a, "listIndex")
          print(a.listIndex(5))