简体   繁体   中英

In Python 3.x how do I create a simple proxy function for another function that is a pure pass through?

In Python 3.x, I want to create a proxy function _proxy for a specific known function proxiedFunc and guarantee that all arguments passed are "forwarded" exactly as if they were passed directly to the proxiedFunc .

# Pseudo-Python code
def _proxy(???generic_parameters???):
        return proxiedFunc(???arguments???)

What I mean by "pure pass through" -> The implementation of the _proxy method should not be affected by (non-)compatible changes to the proxiedMethod , assuming the name of the function doesn't change (SOLID principles). Obviously, callers of _proxy would need to be modified if proxiedMethod is changed incompatibly (ie I'm not intending for _proxy to be an adapter, but that would be a possibility).

The generic way of taking "anything" in a function definition is using *args, **kwargs .

The same syntax is used for passing those args when calling another function.

def _proxy(*args, **kwargs):
        return proxiedFunc(*args, **kwargs)

The single * (eg *args ) captures the positional arguments, and the double (eg **kwargs ) captures the keyword arguments.

args and kwargs are the names you give to those argument-containers. By convention, the name of the "any-positional-args" argument is args or a (its type is tuple ), and the name of the "any-keyword-args" argument is kwargs or kw (its type is dict ).

I, too, wanted to find a way to do that, so I wrote a function for that. I posted it to github: https://github.com/make-itrain/function-proxy

Basically, what it does is:

def a(p1, p2, p3=3, *args, p4=228, p18=11, **kwargs):
    # any function, doesn't matter
    pass


def b(p4, p2, *args):
    # some overlapping arguments
    # it too doesn't matter, what's inside here
    print(p4, p2, args)


args, kwargs = proxy_function(b, a, {"args": ("replaced args",)}, 322, "some_random_arg", 1337, 
                              "arguments", ('anything',), 1, abc=338, cbd="here too?")
b(*args, **kwargs)

Prints 228 some_random_arg ('replaced args',) . Cool, isn't it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM