pythonでオブジェクトが関数かどうかを判断する
1684 ワード
pythonでオブジェクトが関数かどうかを判断する
2つの方法: を判断する. .
また、組み込み関数の表示方法は次のとおりです.
出力:
{‘ArithmeticError’: ArithmeticError, ‘AssertionError’: AssertionError, ‘AttributeError’: AttributeError, ‘BaseException’: BaseException, … ‘print’: < function print>, ‘property’: property, ‘range’: < function range>, ‘raw_input’: < function raw_input>, ‘reduce’: < function reduce>, ‘reload’: < function reload>, }
2つの方法:
hasattr
を用いる、__call__
関数の有無を検証することによりdef test():
print "hello"
print hasattr(test, "__call__")
callable
を用いる判断print callable(test)
また、組み込み関数の表示方法は次のとおりです.
print globals()["__builtins__"].__dict__
出力:
{‘ArithmeticError’: ArithmeticError, ‘AssertionError’: AssertionError, ‘AttributeError’: AttributeError, ‘BaseException’: BaseException, … ‘print’: < function print>, ‘property’: property, ‘range’: < function range>, ‘raw_input’: < function raw_input>, ‘reduce’: < function reduce>, ‘reload’: < function reload>, }