Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54472

Re: I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator.

From Peter Otten <__peter__@web.de>
Subject Re: I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator.
Date 2013-09-20 15:23 +0200
Organization None
References <30bf5567-47ff-4b54-acc4-aebb0451e47d@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.181.1379683376.18130.python-list@python.org> (permalink)

Show all headers | View raw


bab mis wrote:

> def fun:
>     print "entry"
>     .
>     .
>     print "exit"


>>> def log(f):
...     def g(*args, **kw):
...             print "enter", f.__name__
...             try:
...                     return f(*args, **kw)
...             finally:
...                     print "exit", f.__name__
...     return g
... 
>>> @log
... def divide(a, b):
...     return a / b
... 
>>> divide(4, 2)
enter divide
exit divide
2
>>> divide(4, 0)
enter divide
exit divide
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 5, in g
  File "<stdin>", line 3, in divide
ZeroDivisionError: integer division or modulo by zero

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator. bab mis <babmis307@gmail.com> - 2013-09-20 05:59 -0700
  Re: I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator. Peter Otten <__peter__@web.de> - 2013-09-20 15:23 +0200
  Re: I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator. bab mis <babmis307@gmail.com> - 2013-09-22 11:57 -0700
    Re: I want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator. Terry Reedy <tjreedy@udel.edu> - 2013-09-22 16:48 -0400

csiph-web