I have written a the following python code to analyse a function or a method. I am having a script which is having a python function (add) and a class (MyClass) with a method (multiply).When I analyse it using the following method(analyze_func) , I get an output like below.
The problem I am having is, I need to get the value 'MyClass.multiply' from the obj. When I do obj.__name__ , I am only getting "multiply" only.So can anyone propose me how to get the value 'MyClass.multiply'.
-----------------------------------------------
# OUTPUT
-----------------------------------------------
object passed to func() is <unbound method MyClass.multiply>
Method: multiply
===testing code===
result is multiply
===testing code===
Method Arguments: var1 var2
object passed to func() is <function add at 0xb65aa7d4>
Function: add
===testing code===
result is add
===testing code===
Method Arguments: var1 var2
------------------------------------------------
# PYTHON SCRIPT
-----------------------------------------------
Expand|Select|Wrap|Line Numbers
- def analyze_func(obj, method=False):
- str1 = ''
- global result
- print '%s' % obj.__name__
- print 'object passed to func() is ' , obj
- if method:
- print 'Method: %s' % obj.__name__
- strme = '%s' % obj.__name__
- str1=strme
- else:
- print 'Function: %s' % obj.__name__
- strme = '%s' % obj.__name__
- str1=strme
- arginfo = inspect.getargspec(obj)
- args = arginfo[0]
- argsvar = arginfo[1]
- print ''
- print '===testing code==='
- a = arginfo[0]
- print 'result is ' , str1
- result = result + str1
- result = result + '@'
- print '===testing code==='
- print ''
- if args:
- if args[0] == 'self':
- print '\t%s is an instance method' % obj.__name__
- args.pop(0)
- print '\tMethod Arguments: %s' % ' '.join(args)
- if arginfo[3]:
- dl = len(arginfo[3])
- al = len(args)
- defargs = args[al-dl:al]
- print 'Default arguments:',zip(defargs, arginfo[3])
- if arginfo[1]:
- print '\t Positional Args Param: %s' % arginfo[1]
- if arginfo[2]:
- print '\t Keyword Args Param: %s' % arginfo[2]