如何创建一个带有另一个签名的新方法 [英] how to create a new method with signature of another

查看:109
本文介绍了如何创建一个带有另一个签名的新方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个类复制一个方法的签名,并在另一个类中创建一个具有相同签名的代理方法?

我正在写一个RPC python库。
服务器支持远程调用服务器端类(C)。
当客户端连接到服务器时,它应该为具有相同签名的C创建一个代理类。
当程序调用代理实例时,它应该在服务器上调用具有相同参数的函数。

解决方案

考虑使用 boltons.wraps - 以下是文档摘录:


boltons.funcutils.wraps(func,inject = None,** kw)

通过内置的functools.wraps()建模,该函数用于
,使您的装饰器的包装函数反映包装的
函数的结果:



名称文档模块签名



内置functools.wraps()复制前三个,但不复制签名。这个版本的包装可以完全复制内部
函数的签名,从而可以无缝地使用和
自省。用法与内置版本相同:

 >>> from boltons.funcutils import wrapps 
>>>
>>> def print_return(func):
... @wraps(func)
... def wrapper(* args,** kwargs):
... ret = func(* args, ** kwargs)
... print(ret)
... return ret
... return wrapper
...
>>> @ print_return
... def example():
...'''docstring'''
...返回'返回值'
>>>> ;
>>> val = example()
示例返回值
>>>例如.__ name__
'例子'
>>>例如.__ doc__
'docstring'

另外,wrapton的版本支持修改基于内部签名的外部
签名。通过传入注入
参数名称的列表,这些参数将从外部
封装器的签名中移除,从而允许您的装饰器提供不会传入
的参数。



参数:func(function) - 可复制属性为
的可调用对象。



注入(列表) - 新的包装器签名中不应出现
的参数名称的可选列表。

update_dict(bool) - 是否复制其他非标准属性
函数传递给包装器。默认为True。



inject_to_varkw(bool) - 当** kwargs类型的
catch-all存在时,忽略缺少的参数。默认为True。



有关函数的更深入包装,请参阅函数构造器类型
,其中构建了包装。



How can I copy the signature of a method from one class, and create a "proxy method" with same signature in another ?.

I am writing a RPC library in python. The server supports remote calls to a server-side class (C). When the client connects to the server, it should create a proxy class for C with same signatures. When the program calls proxy instance, it should call the function with same arguments at the server.

解决方案

Consider using boltons.wraps - here's an excerpt from the documentation:

boltons.funcutils.wraps(func, injected=None, **kw)

Modeled after the built-in functools.wraps(), this function is used to make your decorator’s wrapper functions reflect the wrapped function’s:

Name Documentation Module Signature

The built-in functools.wraps() copies the first three, but does not copy the signature. This version of wraps can copy the inner function’s signature exactly, allowing seamless usage and introspection. Usage is identical to the built-in version:

>>> from boltons.funcutils import wraps
>>>
>>> def print_return(func):
...     @wraps(func)
...     def wrapper(*args, **kwargs):
...         ret = func(*args, **kwargs)
...         print(ret)
...         return ret
...     return wrapper
...
>>> @print_return
... def example():
...     '''docstring'''
...     return 'example return value'
>>>
>>> val = example()
example return value
>>> example.__name__
'example'
>>> example.__doc__
'docstring'

In addition, the boltons version of wraps supports modifying the outer signature based on the inner signature. By passing a list of injected argument names, those arguments will be removed from the outer wrapper’s signature, allowing your decorator to provide arguments that aren’t passed in.

Parameters: func (function) – The callable whose attributes are to be copied.

injected (list) – An optional list of argument names which should not appear in the new wrapper’s signature.

update_dict (bool) – Whether to copy other, non-standard attributes of func over to the wrapper. Defaults to True.

inject_to_varkw (bool) – Ignore missing arguments when a **kwargs-type catch-all is present. Defaults to True.

For more in-depth wrapping of functions, see the FunctionBuilder type, on which wraps was built.

这篇关于如何创建一个带有另一个签名的新方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆