Skip to content

Instantly share code, notes, and snippets.

@pistatium
Created September 24, 2014 10:47
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 pistatium/a87c8396fa933d29923b to your computer and use it in GitHub Desktop.
Save pistatium/a87c8396fa933d29923b to your computer and use it in GitHub Desktop.
サブクラスの要素だけを一覧で取得する ref: http://qiita.com/kimihiro_n/items/752be1203a3938f73316
class Base(object):
def getAttributeKeys(self):
""" Baseを継承したクラスで定義された
要素(メソッドやプロパティ)のキーだけを取得したい
"""
class Child(Base):
def __init__(self):
self.hoge = "hoge"
def fuga(self):
pass
ch = Child()
print(ch.getAttributeKeys()) # >>> ['hoge', 'fuga']
class Base(object):
def getAttributeKeys(self):
return set(dir(self.__class__)) - set(dir(Base))
ch = Child()
keys = getAttributeKeys()
# attributeの値を取得してみる
key = keys[0] # keysの中から好きに選んだとする
attr = ch.getattr(self, key)
# メソッドかどうかを判定して実行
import types
if isinstance(attr, types.MethodType):
# attrはメソッドなので実行可能
attr()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment