pythonでオブジェクトが関数かどうかを判断する

1684 ワード

pythonでオブジェクトが関数かどうかを判断する
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>, }