Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decorating a function and adding functionalities preserving the number of argument

I'd like to decorate a function, using a pattern like this:

def deco(func):
    def wrap(*a,**kw):
        print "do something"
        return func(*a,**kw)
    return wrap

The problem is that if the function decorated has a prototype like that:

def function(a,b,c): return

When decorated, the prototype is destroyed by the varargs, for example, calling function(1,2,3,4) wouldn't result in an exception. Is that a way to avoid that? How can define the wrap function with the same prototype as the decorated (func) one?

There's something conceptually wrong?

EDIT

My perverse idea was to lighten the "calling of the parent method" without modifying the signature. Something like

def __init__(self, something)
    super(ClassName, self).__init__(something)

to:

@extended
def __init__(self, something):
    ...

I was figuring out if this was possible and if this makes sense.

EDIT As Alex pointed out, the following code doesn't give an exception:

function(1,2,3,4)
like image 770
pygabriel Avatar asked Nov 25 '25 21:11

pygabriel


2 Answers

You're wrong when you state that "calling function(1,2,3,4) wouldn't result in an exception". Check it out:

>>> def deco(f):
...   def w(*a, **k):
...     print 'do something'
...     return f(*a, **k)
...   return w
... 
>>> def f(a, b, c): return
... 
>>> f(1, 2, 3, 4)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: f() takes exactly 3 arguments (4 given)
>>> decorated = deco(f)
>>> decorated(1, 2, 3, 4)
do something
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in w
TypeError: f() takes exactly 3 arguments (4 given)

As you see, you get exactly the same exception as when similarly calling the undecorated f (albeit after the print you've added).

For preserving the wrapped function's metadata (name, docstring, use functools.wraps. Predicting that the wrapped function will raise when called (to avoid doing other work before calling it) is always hard ("impossible" in general, as it's equivalent to the halting problem; "just hard" in specific, when you only care about raising a type-error for argument name and number mismatches and want to treat that specialized exception case differently from any other exception case -- a peculiar requirement;-0).

If you're adamant that you absolutely need it (and can perhaps explain why?) I'll be glad (well, not glad, but will grit my teeth and do it, after all I've got a long weekend in front of me;-) to take you down that labyrinthine path.

like image 55
Alex Martelli Avatar answered Nov 28 '25 10:11

Alex Martelli


The decorator module helps you create a decorator that preserves the function signature.

As a result, you will get the exception you expect when calling the function, and inspect.getargspec will give you the correct signature.

It works by dynamically building a function definition and using exec. Unfortunately, there isn't an easier built-in way to do this.

like image 35
interjay Avatar answered Nov 28 '25 10:11

interjay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!