Skip to content

Instantly share code, notes, and snippets.

@yuuichi-fujioka
Last active December 16, 2015 05:39
Show Gist options
  • Save yuuichi-fujioka/5386129 to your computer and use it in GitHub Desktop.
Save yuuichi-fujioka/5386129 to your computer and use it in GitHub Desktop.
クラスからメソッド取得
class A:
def aaa(self):
''' instance method'''
print 'hello world'
@classmethod
def bbb():
'''class method'''
print 'this is pen'
a = A()
aaa = getattr(a, 'aaa')
aaa() # hello world
aaa2 = getattr(A, 'aaa')
aaa2() # !ERROR
aaa2(a) # hello world
bbb = getattr(b, 'bbb')
bbb() # this is pen
bbb(b) # this is pen
bbb2 = getattr(B, 'bbb')
bbb2() # this is pen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment