def magic_set(obj):
 def decorator(func):
  is_class = isinstance(obj, six.class_types)
  (args, varargs, varkw, defaults) = inspect.getargspec(func)
  if ((not args) or (args[0] not in ('self', 'cls', 'klass'))):
   if is_class:
    replacement = staticmethod(func)
   else:
    replacement = func
  elif (args[0] == 'self'):
   if is_class:
    replacement = func
   else:
    def replacement(*args, **kw):
     return func(obj, *args, **kw)
    try:
     replacement.__name__ = func.__name__
    except:
     pass
  elif is_class:
   replacement = classmethod(func)
  else:
   def replacement(*args, **kw):
    return func(obj.__class__, *args, **kw)
   try:
    replacement.__name__ = func.__name__
   except:
    pass
  setattr(obj, func.__name__, replacement)
  return replacement
 return decorator

def magic_set ( obj ) : def decorator ( func ) : is_class = isinstance ( obj , six . class_types ) ( args , varargs , varkw , defaults ) = inspect . getargspec ( func ) if ( ( not args ) or ( args [ 0 ] not in ( 'self' , 'cls' , 'klass' ) ) ) : if is_class : replacement = staticmethod ( func ) else : replacement = func elif ( args [ 0 ] == 'self' ) : if is_class : replacement = func else : def replacement ( * args , ** kw ) : return func ( obj , * args , ** kw ) try : replacement . __name__ = func . __name__ except : pass elif is_class : replacement = classmethod ( func ) else : def replacement ( * args , ** kw ) : return func ( obj . __class__ , * args , ** kw ) try : replacement . __name__ = func . __name__ except : pass setattr ( obj , func . __name__ , replacement ) return replacement return decorator
adds a function/method to an object .