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


Groups > comp.lang.python > #54599

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 Terry Reedy <tjreedy@udel.edu>
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-22 16:48 -0400
References <30bf5567-47ff-4b54-acc4-aebb0451e47d@googlegroups.com> <8aaaf0ab-09e0-4772-a906-333572320228@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.250.1379882939.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 9/22/2013 2:57 PM, bab mis wrote:
> def funlog(f):
>      def g(*args,**kw):
>          print "enter", f.__name__
>          try:
>              return f(*args, **kw)
>          finally:
>              print "exit", f.__name__
>      return g
>
> class Action:
>      def __init__(self):
>          pass
>
>      @funlog
>      def sret(self):
>          pass
>
>      @funlog
>      def ping(self):
>          pass
>      def verifyerun(self):
>          pass
>
>
> 1) funlog is called for the last function as well.

If this is a question, no, it is not called on verifyerun.

> 2) Is it a proper way to define decorator out of class and use it .

Yes. It is not an Action methods. If you use it in multiple files, 
define once in one file and import it.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious 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