のメソッドを宣言しなさい私は、Python 3.5で問題を得ました。私は別のクラス(A)から継承するクラス(B)を持っています。私はBでのみ宣言されたメソッドを取得しようとしているが、それはここではクラスAだけ(継承されたクラスのメソッドを除く)クラス
コードからクラスBで宣言されたメソッドも継承されたメソッドを返します。
class A:
def a_method_1(self):
""" WILL BE OVERRIDEN """
return True
def a_method_2(self):
""" WON'T BE OVERRIDEN """
return True
class B(A):
def b_method_1(self):
""" NOT OVERRIDEN """
return True
def get_methods(self):
"""
NOT OVERRIDEN
This function filters special python methods.
"""
methods = [method for method in dir(self) if callable(getattr(self, method))
and not method[:2] == '__']
return methods
def a_method_1(self):
""" OVERRIDEN """
return True
if __name__ == "__main__":
B_obj = B()
print(B_obj.get_methods())
戻り値:
>>> ['a_method_1', 'a_method_2', 'b_method_1', 'get_methods']
を
そして、私が望む:
>>> ['a_method_1', 'b_method_1', 'get_methods']
どのように私はget_meを変更することができます継承されたメソッドをフィルタリングするthods?
は良い一日を、 ありがとうございました。
は、あなただけのリストcomphrensionをフィルタリングしますか、またはあなたが完全にクラスからの任意の望ましくない継承されたメソッドを削除したいのですか?私はリスト内包をフィルタリングしたいReti43 @ – Reti43
。 – vmele