I want to print entry and exit message for functions , i have bunchof such function . How can i do i

P

Peter Otten

bab said:
def fun:
print "entry"
.
.
print "exit"
.... def g(*args, **kw):
.... print "enter", f.__name__
.... try:
.... return f(*args, **kw)
.... finally:
.... print "exit", f.__name__
.... return g
........ def divide(a, b):
.... return a / b
....enter divide
exit divide
2enter 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
 
B

bab mis

def fun:

print "entry"

.

.

print "


def fun:

print "entry"

.

.

print "exit"



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 .
 
T

Terry Reedy

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top