Created
April 10, 2019 12:33
-
-
Save eagafonov/39f10af5800de55b001dcdc73fb90f34 to your computer and use it in GitHub Desktop.
Check for bound/unbound class method (python2.7)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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