怎么判断一个变量类型是不是function

快乐田伯光 2011-01-06 04:18:03
判断一个列表如下:
a = []
type(a) == list
True

判断一个变量类型是函数?

def a():
pass


type(a) == function
这样不行,我想知道函数类型是那个关键字

我现在的解决方法是定义另一个空的function用来判断

def b():
pass

type(a) == type(b)
True
...全文
246 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
看着奢扣 2011-01-06
  • 打赏
  • 举报
回复
应该是返回值的类型吧 取决于你的方法返回的是什么。所以不能是function
angel_su 2011-01-06
  • 打赏
  • 举报
回复
callable()
I_NBFA 2011-01-06
  • 打赏
  • 举报
回复
还有你注意,method,function和built-in对象的类型是不同的。
type(print) != type(yourfuns)
I_NBFA 2011-01-06
  • 打赏
  • 举报
回复
在types模块里了
types.FunctionType
types.LambdaType
The type of user-defined functions and functions created by lambda expressions.

types.MethodType
The type of methods of user-defined class instances.

types.BuiltinFunctionType
types.BuiltinMethodType
The type of built-in functions like len() or sys.exit(), and methods of built-in classes. (Here, the term “built-in” means “written in C”.)

其实你内办法就很好,定义个空函数
def tfun(): pass;
tf = type(tfun)
然后type(yourfuns) == tf比就是了,type(yourfuns) is tf也行。

37,721

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧