python 中判斷一個對象是否爲函數

python 中判斷一個對象是否爲函數

兩種方式:

  1. 使用 hasattr ,通過驗證有沒有__call__函數進行判斷
def test():
	print "hello"
print hasattr(test, "__call__")
  1. 通過使用 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>,
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章