0
import sys def entryExit(f): def new_f(self,*args): print "Entering", f.__name__,self.__class__.__name__ f(self,*args) print "Exited", f.__name__,self.__class__.__name__ return new_f class A: @entryExit def move(self,g,h): print "hello" print g,h a=A() a.move(5,7) 

The above code outputs

Entering move A hello 5 7 Exited move A 

I was able to retrieve the method name as well as the class in which it is define using decorators. But i wasnt able to retrieve the name of the method parameters during runtime.

Basically i need an output like this

Entering move A g,h hello 5 7 Exited move A g,h 

So what should i add in the print statements to get an output like above.

Thank you

4
  • All you are doing with your print statement is printing the method name and class name, you aren't telling it print your parameters g, h Commented Mar 14, 2013 at 14:00
  • @PurityLake -- I think that OP wants to know how to figure out that the arguments are named g and h Commented Mar 14, 2013 at 14:03
  • 2
    FYI, self is redundant and actually makes your decorator less flexible in this case. Just have (*args, **kwargs) on the wrapper; the implicit self will be unpacked correctly when needed for instance methods, but you don't force all methods that the decorator is used on to have at least one argument any more. Commented Mar 14, 2013 at 14:04
  • @sr2222 -- I'm kind of surprised that there isn't something in the standard library (functools maybe?) which would transform the call signature of the function... Commented Mar 14, 2013 at 14:10

1 Answer 1

3

You can use inspect.getargspec for this.

e.g.:

import sys import inspect def entryExit(f): def new_f(self,*args): print "Entering", f.__name__,self.__class__.__name__,inspect.getargspec(f).args[1:] f(self,*args) print "Exited", f.__name__,self.__class__.__name__ return new_f class A: @entryExit def move(self,g,h): print "hello" print g,h a=A() a.move(5,7) 

results in:

Entering move A ['g', 'h'] hello 5 7 Exited move A 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.