Hi,
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
-----------------------------------------------

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

To the extent I understand your question, it seems to me that instead of obj.__name__ you should grab repr(obj) . The default __repr__() method would return something like '<bound method MyClass.multiply of <__main__.MyClass instance at 0x009E8968>>' which is not quite what you want -- but note it is a string you can parse to extract the information you need.

This article has been dead for over six months. Start a new discussion instead.