😸
[Python]関数がLambda関数であるかどうかを調べたい
概要
- types.LambdaTypeで型チェックは可能だが、types.LambdaTypeはtypes.FunctionTypeのaliasなので
__name__
が<lambda>
であることもチェックする必要あり -
It's worth noting that types.LambdaType is an alias for types.FunctionType. There's no (easy or reliable) way to tell the difference between a lambda function and one created with def
- したがってこのように書く必要がある
import types
def is_lambda_function(obj):
return isinstance(obj, types.LambdaType) and obj.__name__ == "<lambda>"
Discussion