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>,
}