1

Consider the following Python program:

a = lambda x: x
b = lambda x: x
print(a == b)

This obviously outputs False although, which is clear why it happens. However, this is counterintuitive. I wonder if there is any programming language (not academic-only) that is able to do a structural comparison of lambda expressions and would print True in the example above? If this doesn't work out of the box, is there any smart way to compare lambda expressions at all? How about the .NET abstract syntax trees?

Edit: As I got the answer, here is working example:

# Applies an argument arg to a function f.
apply = lambda f, arg: lambda *args, **kwargs: f(arg, *args, **kwargs)
# Compare two functions by their co_code (see answer below)
equals = lambda l0, l1: l0.__code__.co_code == l1.__code__.co_code

# Defines a function that adds 2 to a provided number.
add_two_0 = apply(add, 2)

# Another way of adding two is twice adding 1.
add_two_1 = apply(apply(add, 1), 1)

# The following statement prints True
equals(add_two_0, add_two_1)
0

3 Answers 3

1

Comparing the code objects won't do what you want: two different functions will have different code objects.

You could compare the bytecode of each function, though, which you can get with co_code:

(lambda x:x).__code__.co_code
# b'|\x00\x00S'
(lambda y:y).__code__.co_code
# b'|\x00\x00S'


(lambda y:y+1).__code__.co_code
# b'|\x00\x00d\x01\x00\x17S'
(lambda y:y-1).__code__.co_code
# b'|\x00\x00d\x01\x00\x18S'

So, your comparisons would give, as expected:

(lambda x:x).__code__.co_code == (lambda y:y).__code__.co_code
# True
(lambda y:y+1).__code__.co_code == (lambda y:y-1).__code__.co_code
# False
Sign up to request clarification or add additional context in comments.

Comments

1

You don't need another programming language. Python can do it.

>>> (lambda x: x + 1).__code__ == (lambda x: x+1).__code__
True

P.S.: I didn't downvote your question. In fact, I think your question is valid. It's probably someone who newly acquired the ability to downvote. Don't worry about it; one-two reputation points don't matter much.

4 Comments

Your statement evaluates to True, which is interesting. However, in my example above, I get the result that I would have expected: a.__code__ == b.__code__ evaluates to False.
I am afraid I don't understand your comment. Do you expect a.__code__ == b.__code__ to evaluate to False?
I just tested it and a.__code__ == b.__code__ evaluates to false. I expected this outcome because of how Python works. Of course, I asked for a way to make it evaluate to true. Using the code function doesn't help, because it does a kind of stupid comparison. For example, try (lambda x: x).__code__ == (lambda y: y).__code__ which evaluates to False as well although it the same function.
Let me make this a bit more clear: a == b evaluates to False. I expected this, but it's not what I am looking for. a.__code__ == b.__code__ evaluates to False. I also expected this, but it's not what I am looking for. (lambda x: x + 1).__code__ == (lambda x: x+1).__code__ evaluates to True. I didn't expect this, this could be what I am looking for, but it is not, because just renaming the variables makes it evaluate to False like in (lambda x: x).__code__ == (lambda y: y).__code__
0

Is this what you are looking for?

a = lambda x: x
b = lambda x: x
print(a.__code__ == b.__code__)

1 Comment

Well, that prints True, but a = lambda x: x b = lambda y: y print(a.__code__ == b.__code__) prints False, which is not what I am looking for, because a and b are still equal.

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.