Discuss / Python / 判断是否为函数

判断是否为函数

Topic source

岁益寒

#1 Created at ... [Delete] [Delete and Lock User]

# -*- coding: utf-8 -*-

import types

def is_func(fn):

    return type(fn) == types.FunctionType or type(fn) == types.BuiltinFunctionType or type(fn) == types.LambdaType  \

        or type(fn) == types.GeneratorType

print(is_func(abs), is_func(is_func), is_func(lambda x: x**2))

廖雪峰

#2 Created at ... [Delete] [Delete and Lock User]
>>> callable(abs)
True

  • 1

Reply