[BangPypers] decorator help
Dhananjay Nene
dhananjay.nene at gmail.com
Fri Sep 20 16:12:44 CEST 2013
On Fri, Sep 20, 2013 at 6:06 PM, babmis <babmis at outlook.com> wrote:
> I want to print entry and exit message for functions , i have bunch of such
> function . How can i do it in decorator.
>>> from functools import wraps
>>>
>>> def trace(f):
... @wraps(f)
... def wrap(*args, **kwargs):
... print("Entering {} with {} {}".format(f.__name__,args,kwargs))
... ret = f(*args,**kwargs)
... print("Leaving {} with {}".format(f.__name__, ret))
... return ret
... return wrap
...
>>> @trace
... def foo(arg1,arg2,arg3="bar",arg4="buz") :
... print("Into foo")
... return ("Zoo", False)
...
>>>
>>> foo("hello", "world", arg4="Boo!")
Entering foo with ('hello', 'world') {'arg4': 'Boo!'}
Into foo
Leaving foo with ('Zoo', False)
('Zoo', False)
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
More information about the BangPypers
mailing list