18

I'm working on a project, which contains several modules. Simplifying the problem, there is some variable x. Sometimes it may be int or float or list. But it may be a lambda function, and should be treated in different way. How to check that variable x is a lambda?

For example

>>> x = 3
>>> type(x)
<type 'int'>
>>> type(x) is int
True
>>> x = 3.4
>>> type(x)
<type 'float'>
>>> type(x) is float
True
>>> x = lambda d:d*d
>>> type(x)
<type 'function'>
>>> type(x) is lambda
  File "<stdin>", line 1
    type(x) is lambda
                    ^
SyntaxError: invalid syntax
>>> type(x) is function
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'function' is not defined
>>> 
1

3 Answers 3

31

You need to use types.LambdaType or types.FunctionType to make sure that the object is a function object like this

x = lambda d:d*d
import types
print type(x) is types.LambdaType
# True
print isinstance(x, types.LambdaType)
# True

and then you need to check the name as well to make sure that we are dealing with a lambda function, like this

x = lambda x: None
def y(): pass
print y.__name__
# y
print x.__name__
# <lambda>

So, we put together both these checks like this

def is_lambda_function(obj):
    return isinstance(obj, types.LambdaType) and obj.__name__ == "<lambda>"

As @Blckknght suggests, if you want to check if the object is just a callable object, then you can use the builtin callable function.

Sign up to request clarification or add additional context in comments.

8 Comments

isinstance(x, types.LambdaType) looks more idiomatic.
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.
@Blckknght That is why we need to check the __name__ as well :-)
Ah, I see I hadn't payed enough attention to the end of the answer, and you've already addressed the issue of lambdas versus regular functions. I'd suggest though that there probably isn't much need to tell the difference. If your code really cares how a given function was defined, you're probably doing it wrong. Most of the time you should use "duck typing" rather than strict type checks. If you expect a function, accept anything that is callable (such as a class, or an instance of a class with a __call__ method). If you do need to check up front, use the builtin callable function.
__name__ can be changed. Alternatively, use __code__.co_name, which contains the name of the function/lambda at the time it was compiled and is read-only. See stackoverflow.com/questions/54094263/…
|
2

If you prefer the typing module, use Callable:

In [1]: from typing import Callable
In [2]: isinstance(lambda: None, Callable)
Out[2]: True

8 Comments

what is the difference between your solution and @Blckknght suggestion to use callable built-in function?
@rth you can't use callable() for typing hinting your signatures
A regular function (not lambda) also returns True: def x(): pass, isinstance(x, Callable) ->True. So I don't see any advantage of your answer vs @thefourtheye one.
1) your question should specify that you want to discriminate between lambda functions and ordinary functions if that is a requirement. 2) callable(lambda: None) == True
I don't know how to make more specific, it is in the title How to check that variable is a lambda function :) Thank you for additional answer!
|
0

Can consider the following:

type(x) == type(lambda x:x)

1 Comment

Welcome to Stack Overflow! Please read How to Answer and edit your answer to contain an explanation as to why this code would actually solve the problem at hand. Always remember that you're not only solving the problem, but are also educating the OP and any future readers of this post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.