Resuming the answers and comments from the duplicated:
import types
types.FunctionType(code_obj, globals={}, name='x')(1)
To work with methods, you can use a function type or an unbound method and then pass an instance as first parameter, or bound the function to an instance:
class A(object):
def __init__(self, name):
self.name = name
def f(self, param):
print self.name, param
# just pass an instance as first parameter to a function or to an unbound method
func = types.FunctionType(A.f.__code__, globals={}, name='f')
func(A('a'), 2)
unbound_method = types.MethodType(func, None, A)
unbound_method(A('b'), 3)
# or bound the function to an instance
bound_method = types.MethodType(func, A('c'), A)
bound_method(4)