Skip to content

Instantly share code, notes, and snippets.

@eagafonov
Created April 10, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eagafonov/39f10af5800de55b001dcdc73fb90f34 to your computer and use it in GitHub Desktop.
Save eagafonov/39f10af5800de55b001dcdc73fb90f34 to your computer and use it in GitHub Desktop.
Check for bound/unbound class method (python2.7)
# class for autopsy
In [1]: class A:
...: def foo():
...: pass
# initial investigations
In [16]: str(A.foo)
Out[16]: '<unbound method A.foo>'
In [17]: str(A().foo)
Out[17]: '<bound method A.foo of <__main__.A instance at 0x7fb8508eb830>>'
# Open CPython code, look for strings "<bound method" and "<unbound method"
# https://github.com/python/cpython/blob/2.7/Objects/classobject.c#L2415
# Bingo! check im_self propert of the method
In [29]: A.foo.im_self is None
Out[29]: True
In [31]: A().foo.im_self
Out[31]: <__main__.A instance at 0x7fb850936098
In [30]: A().foo.im_self is None
Out[30]: False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment