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

Discussion in 'Python' started by bab mis, Sep 20, 2013.

  1. bab mis

    bab mis Guest

    def fun:
    print "entry"
     
    bab mis, Sep 20, 2013
    #1
    1. Advertisements

  2. bab mis

    Peter Otten Guest

    .... 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
     
    Peter Otten, Sep 20, 2013
    #2
    1. Advertisements

  3. bab mis

    bab mis Guest







    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 .
     
    bab mis, Sep 22, 2013
    #3
  4. bab mis

    Terry Reedy Guest

    If this is a question, no, it is not called on verifyerun.
    Yes. It is not an Action methods. If you use it in multiple files,
    define once in one file and import it.
     
    Terry Reedy, Sep 22, 2013
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.