0

Say I have the following class

class Test:
    def TestFunc(self):
        print 'this is Test::TestFunc method'

Now, I create an instance of the class Test

>>> 
>>> t = Test()
>>> 
>>> t
<__main__.Test instance at 0xb771b28c>
>>> 

Now, the t.TestFunc is represented as follows

>>> 
>>> t.TestFunc
<bound method Test.TestFunc of <__main__.Test instance at 0xb771b28c>>
>>> 

Now I am storing the Python representation of t.TestFunc to a string string_func

>>> 
>>> string_func = str(t.TestFunc)
>>> string_func
'<bound method Test.TestFunc of <__main__.Test instance at 0xb771b28c>>'
>>> 

Now, Is there a way, where I can get the function handle from the string <bound method Test.TestFunc of <__main__.Test instance at 0xb771b28c>>. For example,

>>> 
>>> func = xxx(string_func)
>>> func 
<bound method Test.TestFunc of <__main__.Test instance at 0xb771b28c>>
>>> 
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
  • 3
    If you want to serialize the objects into strings use [`pickle`](http://docs.python.org/2/library/pickle.html). – Bakuriu Feb 19 '13 at 22:18
  • 1
    You could build an `id` dictionary from `globals()` or `gc.get_objects()` or something, and then get the instance from that, and then use `getattr` to get the method from the instance, but it'd be awfully ugly. – DSM Feb 19 '13 at 22:20
  • This is a duplicate: http://stackoverflow.com/questions/14968349/textual-reference-of-a-method – JCash Feb 19 '13 at 22:57
  • What is the actual use case here? This smells like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). If you had a magic way to get from bound method `repr`s back to the original bound method (which even somehow solved the obvious problems like the bound instance no longer existing…), what would you use it for? – abarnert Feb 19 '13 at 23:59
  • If you're just looking for a way to get `t` and `TestFunc` back from `f` after doing something like `f = t.TestFunc`, that's easy. (It's `f.__self__` and `f.__func__` in 3.x, `f.im_self` and `f.im_func` in 2.x.) So, if you're trying to use the `repr` as a convoluted way to get that information, you're doing it wrong. – abarnert Feb 20 '13 at 00:02
  • Finally, you seem to be confused about the difference between a function and a bound method, because you refer to "get the function handle" but then your desired example gets the bound method instead. – abarnert Feb 20 '13 at 00:03

3 Answers3

4

You cannot with the string alone go back to the same object, because Python does not give you a method to look up objects by memory address.

You can go back to another instance of __main__.Test, provided it's constructor doesn't take any arguments, and look up the method again, but it will not have the same memory address.

You'd have to parse the string for it's components (module, classname, and method name), then use getattr() on the various components, instantiating the class as part of the process. I doubt this is what you wanted though.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

There are several pitfalls to consider:

  • the instance of Test may or may not exist anymore
  • the instance may have been garbage collected
  • the instance may have had the function monkey-patched Test.TestFunc
  • a different object may have been created at 0xb771b28c
dnozay
  • 23,846
  • 6
  • 82
  • 104
0

You can use getattr.

    In [1]:
    class Test:
        def TestFunc(self):
            print 'this is Test::TestFunc method'

    In [2]: t = Test()

    In [3]: getattr(t, 'TestFunc')
    Out[3]: <bound method Test.TestFunc of <__main__.Test instance at 0xb624d68c>>

    In [4]: getattr(t, 'TestFunc')()
    this is Test::TestFunc method
Noel Evans
  • 8,113
  • 8
  • 48
  • 58
  • 1
    Read again. That's not the question, OP wants to go from `str(t.TestFunc)` back to `t`. Also, your use of `getattr` (with constant strings) is worse than pointless. –  Feb 19 '13 at 22:25