Q: We have *args and **kwargs. Woud ***allargs be useful?

Jonathan Fine J.Fine at open.ac.uk
Thu Apr 1 12:44:02 EDT 2010


Jon Clements wrote:

> I'm not sure this'll catch on, it'll be interesting to see other
> comments.
> However, I believe you can get the behaviour you desire something
> like:
> 
> import inspect
> 
> class AllArgs(object):
>     def __init__(self, func):
>         self._func = func
>         self._spec = inspect.getargspec(func)
>         self._nposargs = len(self._spec.args)
>     def __call__(self, *args, **kwdargs):
>         self._func.func_globals['Args'] = (args[self._nposargs:],
> kwdargs)
>         return self._func(*args[:self._nposargs])
> 
> @AllArgs
> def test():
>     print Args
> 
> @AllArgs
> def test2(a, b):
>     print a, b, Args
> 
> test(1, 2, 3, 4, 5, a=3, b=5)
> test2(1, 2, 3, 4, 5, c=7)
> 
> Done quickly, probably buggy, but does provide 'Args', but without
> further work
> swallows any *'s and **'s and might ignore defaults (hideously
> untested)

Thank you for your interest, Jon.

According to http://docs.python.org/library/inspect.html we have that 
func_globals is the global namespace in which this function was defined.

Hence we have
 >>> def f(): pass
...
 >>> f.func_globals is globals()
True
 >>> f.func_globals ['x'] = 3
 >>> x
3
 >>>

I don't yet understand what your code is intended to do, but I'm fairly 
sure you're not wishing to 'monkey-patch' a global namespace of a module.

-- 
Jonathan



More information about the Python-list mailing list