本文整理汇总了Python中types.FunctionType.__dict__方法的典型用法代码示例。如果您正苦于以下问题:Python FunctionType.__dict__方法的具体用法?Python FunctionType.__dict__怎么用?Python FunctionType.__dict__使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类types.FunctionType
的用法示例。
在下文中一共展示了FunctionType.__dict__方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: buildFunction
# 需要导入模块: from types import FunctionType [as 别名]
# 或者: from types.FunctionType import __dict__ [as 别名]
def buildFunction(baseFunc, code=None, glbls=None,
name=None, defaults=None,
kwdefaults=None, closure=None,
annotations=None, doc=None, dct=None):
resf = None
def _f():
pass
if hasattr(_f, 'func_code'):
# Python 2.x
resf = FunctionType(code or baseFunc.func_code,
glbls or baseFunc.func_globals,
name or baseFunc.func_name,
defaults or baseFunc.func_defaults,
closure or baseFunc.func_closure)
resf.func_dict = dct or baseFunc.func_dict
resf.func_doc = doc or baseFunc.func_doc
else:
# Python 3.x
resf = FunctionType(code or baseFunc.__code__,
glbls or baseFunc.__globals__,
name or baseFunc.__name__,
defaults or baseFunc.__defaults__,
closure or baseFunc.__closure__)
resf.__kwdefaults__ = kwdefaults or baseFunc.__kwdefaults__
resf.__annotations__ = annotations or baseFunc.__annotations__
resf.__dict__ = dct or baseFunc.__dict__
resf.__doc__ = doc or baseFunc.__doc__
return resf
示例2: handle_deffun
# 需要导入模块: from types import FunctionType [as 别名]
# 或者: from types.FunctionType import __dict__ [as 别名]
def handle_deffun(self, func, fdict, fdoc, remote_globals):
func = self.unpack(func)
g = globals()
glbls = {k:g[k] for k in remote_globals if k in g} if remote_globals is not None else g.copy()
glbls.update(func[1])
func[1].update(glbls)
f = FunctionType(*func)
f.__dict__ = self.unpack(fdict)
f.__doc__ = self.unpack(fdoc)
return self.pack(f)
示例3: loads_function
# 需要导入模块: from types import FunctionType [as 别名]
# 或者: from types.FunctionType import __dict__ [as 别名]
def loads_function(s):
'''Restores a function serialized with :func:`dumps_function`.'''
name, code, globals_, defaults, closure, func_dict, doc, qualname, kwdefaults, annotations = loads(s)
code = marshal.loads(code)
for k, v in globals_.items():
if isinstance(v, Module):
globals_[k] = v.mod
if closure is not None:
import ctypes
ctypes.pythonapi.PyCell_New.restype = ctypes.py_object
ctypes.pythonapi.PyCell_New.argtypes = [ctypes.py_object]
closure = tuple(ctypes.pythonapi.PyCell_New(c) for c in closure)
globals_['__builtins__'] = __builtins__
r = FunctionType(code, globals_, name, defaults, closure)
r.__dict__ = func_dict
r.__doc__ = doc
r.__qualname__ = qualname
r.__kwdefaults__ = kwdefaults
r.__annotations__ = annotations
return r
示例4: rebindFunction
# 需要导入模块: from types import FunctionType [as 别名]
# 或者: from types.FunctionType import __dict__ [as 别名]
def rebindFunction(f, rebindDir=None,
funcName=None, funcDoc=None,
argRebindDir=None, propRebindDir=None,
**rebinds):
'''return a function derived from *f* with rebinds specified by *rebindDir* and/or *rebinds*.
Use *funcName* as function name, instead of `f.func_name` as function
name, if given.
*argRebindDir* is a dictionary mapping function parameter names
to defaults. You can use this to turn required parameters into optional
ones (by providing a default value for them), to change their
default values or to turn optional parameters into required ones.
Note that Python requires that required arguments must preceed
optional ones. A `ValueError` is raised when *argRebindDir* violates
this restriction.
Note: we only support simply named parameters (not constructor expressions).
*propRebindDir* is a dictionary specifying rebinds for the
functions properties.
ATT: *f.func_globals* is copied at rebind time. Later modifications
may affect *f* but not the rebind function.
Note: we would like to rebind closure parts as well but Python currently
does not allow to create `cell` instances. Thus, this would be difficult.
'''
# unwrap a method
f = getattr(f, 'im_func', f)
# handle global variable rebinds
fg = f.func_globals.copy()
if rebindDir: fg.update(rebindDir)
if rebinds: fg.update(rebinds)
# handle argument (default) rebinds
if argRebindDir:
args, _, _, defaults = getargspec(f)
# ensure all arguments are known
unknown = []
for a in argRebindDir:
if a not in args: unknown.append(a)
if unknown:
raise ValueError('unknown arguments in `argRebindDir`: %s'
% ', '.join(unknown)
)
# determine new defaults
defaults = defaults is not None and list(defaults) or []
defaults = [REQUIRED] * (len(args) - len(defaults)) + defaults
funcDefaults = []
for (a,d) in zip(args, defaults):
if isinstance(a, str) and a in argRebindDir: d = argRebindDir[a]
if d is not REQUIRED: funcDefaults.append(d)
elif funcDefaults: raise ValueError('required argument after optional one: %s' % a)
funcDefaults = tuple(funcDefaults)
else: funcDefaults = f.func_defaults or ()
# construct the new function
nf = FunctionType(
f.func_code,
fg, # func_globals
funcName or f.func_name,
funcDefaults,
f.func_closure,
)
# handle the documentation
if funcDoc is not None: nf.func_doc = funcDoc
else: nf.func_doc = f.func_doc
# handle is properties
if f.__dict__ is not None: nf.__dict__ = f.__dict__.copy()
if propRebindDir:
if nf.__dict__ is None: nf.__dict__ = {}
nf.__dict__.update(propRebindDir)
return nf