Created
September 24, 2014 10:47
-
-
Save pistatium/a87c8396fa933d29923b to your computer and use it in GitHub Desktop.
サブクラスの要素だけを一覧で取得する ref: http://qiita.com/kimihiro_n/items/752be1203a3938f73316
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 Base(object): | |
def getAttributeKeys(self): | |
""" Baseを継承したクラスで定義された | |
要素(メソッドやプロパティ)のキーだけを取得したい | |
""" | |
class Child(Base): | |
def __init__(self): | |
self.hoge = "hoge" | |
def fuga(self): | |
pass |
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
ch = Child() | |
print(ch.getAttributeKeys()) # >>> ['hoge', 'fuga'] |
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 Base(object): | |
def getAttributeKeys(self): | |
return set(dir(self.__class__)) - set(dir(Base)) |
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
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