PythonのLambda関数であるかどうかを調べたい
なんで書いたのか
pd.groupbyのaggregate関数のutil関数を作ってる際にutli関数の引数にlambda関数は除外したい
概要
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
code:sample.py
import types
def is_lambda_function(obj):
return isinstance(obj, types.LambdaType) and obj.__name__ == "<lambda>"
参考