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 at gmail.com
Sun Sep 22 14:57:30 EDT 2013


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 .



More information about the Python-list mailing list