😸

[Python]関数がLambda関数であるかどうかを調べたい

2020/09/24に公開

概要

  • 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>"

参考

GitHubで編集を提案

Discussion