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 at udel.edu
Sun Sep 22 16:48:39 EDT 2013


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




More information about the Python-list mailing list