How can I test whether a variable holds a lambda?

前端 未结 5 1509
灰色年华
灰色年华 2021-02-04 00:19

Is there a way to test whether a variable holds a lambda? The context is I\'d like to check a type in a unit test:

self.assertEquals(lambda, type(my         


        
5条回答
  •  不知归路
    2021-02-04 01:13

    Use the types module:

    from types import *
    
    assert isinstance(lambda m: m, LambdaType)
    

    According to the docs, It is safe to use from types import *.

    ATTENTION to the reader: this is wrong! types.LambdaType is types.FunctionType, so the above exrpession will match both Lambdas and Functions, alike.

提交回复
热议问题