Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I treat positional arguments as keyword arguments in Python 2

For a decorator I am writing I would like to manipulate a specific named parameter of a function. Consider the following decorator:

def square_param(param):
    def func_decorator(func):
        def func_caller(*args,**kwargs):
            kwargs[param] = kwargs[param] * kwargs[param]
            return func(*args,**kwargs)
    return func_caller
return func_decorator

Applied on the next function:

@square_param('dividend')
def quotient(divisor=1,dividend=0):
    return dividend/divisor

This will work if dividend is called as a keyword argument e.g.:

>>> quotient(dividend=2)
4

However, when given as a positional argument this will fail.

>>> quotient(3,4)
TypeError: quotient() got multiple values for keyword argument 'dividend'

With Python 3 I could solve this by forcing the parameter to be always given as a keyword:

@square_param('dividend')
def quotient(divisor=1,*,dividend=0):
    return dividend/divisor

but I would like to support Python 2 and also I would like to put as little restrictions on the function.

Is there a way that I can fix this behaviour in my decorator?

like image 648
Peter Smit Avatar asked Jul 05 '11 07:07

Peter Smit


2 Answers

Firstly, your square_param decorator doesn't work because it doesn't return the functions. It needs to be:

def square_param(param):
    def func_decorator(func):
        def func_caller(*args,**kwargs):
            kwargs[param] = kwargs[param] * kwargs[param]
            return func(*args,**kwargs)
        return func_caller
    return func_decorator

Now I took @Dirk's advice and looked into the inspect module. You can do it by checking first if the parameter is one of the function's positional arguments, and second if that positional argument has been specified, and then modifying that argument position. You also need to make sure you only modify kwargs if the parameter was supplied as a keyword argument.

import inspect

def square_param(param):
    def func_decorator(func):
        def func_caller(*args,**kwargs):
            funparams = inspect.getargspec(func).args
            if param in funparams:
                i = funparams.index(param)
                if len(args) > i:
                    args = list(args)   # Make mutable
                    args[i] = args[i] * args[i]
            if param in kwargs:
                kwargs[param] = kwargs[param] * kwargs[param]
            return func(*args,**kwargs)
        return func_caller
    return func_decorator
like image 90
mgiuca Avatar answered Oct 04 '22 04:10

mgiuca


even without using Inspect we can get function params

>>> func = lambda x, y, args: (x, y, {})
>>> func.func_code.co_argcount
3
>>> func.func_code.co_varnames
('x', 'y', 'args')
like image 27
Yajushi Avatar answered Oct 04 '22 05:10

Yajushi