Question on difference between LambdaType and FunctionType

Chris Angelico rosuav at gmail.com
Sun Nov 25 14:41:10 EST 2018


On Mon, Nov 26, 2018 at 6:06 AM Iwo Herka <iwoherka at gmail.com> wrote:
>
> Hello,
>
> Can someone please provide a use-case for having LambdaType
> separate to FunctionType?

Clarification: This is talking about the 'types' module.

> Since they are equal
> (LambdaType == FunctionType) it seems a bit superfluous to me.
> For example, I cannot do the following:
>
>     if type(foo) is FunctionType and not type(foo) is LambdaType:
>         ...
>

I was actually unaware of LambdaType even existing. Obviously it has
to be the same as FunctionType (even more strongly than you put it:
"types.LambdaType is types.FunctionType"), so I agree, it's
superfluous. Maybe it's in case some other Python implementation might
distinguish??

https://docs.python.org/3/library/types.html#types.FunctionType

That said, though: if you want to distinguish a lambda function from a
def function, you can do so with reasonable reliability using the
function's name:

>>> def is_lambda(f): return f.__name__ == "<lambda>"
...
>>> is_lambda(is_lambda)
False
>>> is_lambda(lambda x: x+1)
True

ChrisA



More information about the Python-list mailing list