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


Groups > comp.lang.python > #54469 > unrolled thread

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

Started bybab mis <babmis307@gmail.com>
First post2013-09-20 05:59 -0700
Last post2013-09-22 16:48 -0400
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

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

Frombab mis <babmis307@gmail.com>
Date2013-09-20 05:59 -0700
SubjectI want to print entry and exit message for functions , i have bunch of such function . How can i do it in decorator.
Message-ID<30bf5567-47ff-4b54-acc4-aebb0451e47d@googlegroups.com>
def fun:
    print "entry"
    .
    .
    print "exit"

[toc] | [next] | [standalone]


#54472

FromPeter Otten <__peter__@web.de>
Date2013-09-20 15:23 +0200
Message-ID<mailman.181.1379683376.18130.python-list@python.org>
In reply to#54469
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

[toc] | [prev] | [next] | [standalone]


#54595

Frombab mis <babmis307@gmail.com>
Date2013-09-22 11:57 -0700
Message-ID<8aaaf0ab-09e0-4772-a906-333572320228@googlegroups.com>
In reply to#54469
On Friday, September 20, 2013 6:29:55 PM UTC+5:30, bab mis wrote:
> def fun:
> 
>     print "entry"
> 
>     .
> 
>     .
> 
>     print "


On Friday, September 20, 2013 6:29:55 PM UTC+5:30, bab mis wrote:
> def fun:
> 
>     print "entry"
> 
>     .
> 
>     .
> 
>     print "exit"



On Friday, September 20, 2013 6:29:55 PM UTC+5:30, bab mis wrote:
> def fun:
> 
>     print "entry"
> 
>     .
> 
>     .
> 
>     print "exit"




Peter couple of queries with following code :

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.
2) Is it a proper way to define decorator out of class and use it .

[toc] | [prev] | [next] | [standalone]


#54599

FromTerry Reedy <tjreedy@udel.edu>
Date2013-09-22 16:48 -0400
Message-ID<mailman.250.1379882939.18130.python-list@python.org>
In reply to#54595
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web