(cls, spec)
return types.FunctionType(func, {}, argdefs=defaults)
Generate `__init__` function based on TPayload.default_spec

    For example::

        spec = [('name', 'Alice'), ('number', None)]

    will generate a types.FunctionType object representing::

        def __init__(self, name='Alice', number=None):
            self.name = name
            self.number = number
Generate `__init__` function based on TPayload.default_spec
Generate __init__ function based on TPayload . default_spec
def init_func_generator(cls, spec):
    """Generate `__init__` function based on TPayload.default_spec

    For example::

        spec = [('name', 'Alice'), ('number', None)]

    will generate a types.FunctionType object representing::

        def __init__(self, name='Alice', number=None):
            self.name = name
            self.number = number
    """
    if not spec:
        def __init__(self):
            pass
        return __init__

    varnames, defaults = zip(*spec)

    args = ', '.join(map('{0[0]}={0[1]!r}'.format, spec))
    init = "def __init__(self, {}):\n".format(args)
    init += "\n".join(map('    self.{0} = {0}'.format, varnames))

    name = '<generated {}.__init__>'.format(cls.__name__)
    code = compile(init, name, 'exec')
    func = next(c for c in code.co_consts if isinstance(c, types.CodeType))

    # Add a fake linecache entry so debuggers and the traceback module can
    # better understand our generated code.
    linecache.cache[name] = (len(init), None, init.splitlines(True), name)

    return types.FunctionType(func, {}, argdefs=defaults)
def init_func_generator ( cls , spec ) : if not spec : def __init__ ( self ) : pass return __init__ varnames , defaults = zip ( * spec ) args = ', ' . join ( map ( '{0[0]}={0[1]!r}' . format , spec ) ) init = "def __init__(self, {}):\n" . format ( args ) init += "\n" . join ( map ( '    self.{0} = {0}' . format , varnames ) ) name = '<generated {}.__init__>' . format ( cls . __name__ ) code = compile ( init , name , 'exec' ) func = next ( c for c in code . co_consts if isinstance ( c , types . CodeType ) ) # Add a fake linecache entry so debuggers and the traceback module can # better understand our generated code. linecache . cache [ name ] = ( len ( init ) , None , init . splitlines ( True ) , name ) return types . FunctionType ( func , { } , argdefs = defaults )