4

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 (i.e. I'm not intending for _proxy to be an adapter, but that would be a possibility).

successhawk
  • 3,071
  • 3
  • 28
  • 44

2 Answers2

8

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 * (e.g. *args) captures the positional arguments, and the double (e.g. **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).

shx2
  • 61,779
  • 13
  • 130
  • 153
0

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?

winwin
  • 958
  • 7
  • 25