Skip to content

Instantly share code, notes, and snippets.

@yu-iskw
Last active September 9, 2015 04:40
Show Gist options
  • Save yu-iskw/d8802363a56510bb5470 to your computer and use it in GitHub Desktop.
Save yu-iskw/d8802363a56510bb5470 to your computer and use it in GitHub Desktop.
Mthod Inspection in Python 2.7
import sys

class Foo():
    @staticmethod
    def sm():
        return 1

    @classmethod
    def cm(cls):
        return 1

    def im(self):
        return 1

    @property
    def pm(self):
        return 1


# Not Expected
print("Inspect with type:")
print(type(Foo.sm))
print(type(Foo.cm))
print(type(Foo.im))
print(type(Foo.pm))

# Expected
print("Inspect with __dict__:")
print(type(Foo.__dict__['sm']))
print(type(Foo.__dict__['cm']))
print(type(Foo.__dict__['im']))
print(type(Foo.__dict__['pm']))

klass = getattr(sys.modules[__name__], "Foo")
print(type(klass))

# Not Expected
print("Inspect with type through getattr:")
print(type(getattr(klass, "sm")))
print(type(getattr(klass, "cm")))
print(type(getattr(klass, "im")))
print(type(getattr(klass, "pm")))

# Expected
print("Inspect with __dict__ through getattr:")
print(type(klass.__dict__['sm']))
print(type(klass.__dict__['cm']))
print(type(klass.__dict__['im']))
print(type(klass.__dict__['pm']))
Inspect with type:
<type 'function'>
<type 'instancemethod'>
<type 'instancemethod'>
<type 'property'>
Inspect with __dict__:
<type 'staticmethod'>
<type 'classmethod'>
<type 'function'>
<type 'property'>
<type 'classobj'>
Inspect with type through getattr:
<type 'function'>
<type 'instancemethod'>
<type 'instancemethod'>
<type 'property'>
Inspect with __dict__ through getattr:
<type 'staticmethod'>
<type 'classmethod'>
<type 'function'>
<type 'property'>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment