python 中判斷一個對象是否爲函數
兩種方式:
- 使用
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>,
}