Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lambda loop variable closure fix #3066

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

asishm
Copy link

@asishm asishm commented Nov 4, 2022

Closes #2918

Issue arises because of this issue - https://stackoverflow.com/questions/2295290/what-do-lambda-function-closures-capture

the function generator could use a better name.

@stevenbird
Copy link
Member

stevenbird commented Nov 8, 2022

Thanks for the fix @asishm. How about using map() with a lambda argument to avoid the need to name the function, roughly like this:

map(lambda ..., conds)

@asishm
Copy link
Author

asishm commented Nov 8, 2022

So something like map(lambda cond: lambda sent_index, word_indices: cond, conds) ?

@tomaarsen
Copy link
Member

That seems to work!

conds = "iterable"

print("Current approach")
functions = [lambda sent_index, word_indices: cond for cond in conds]
for function in functions:
    print(function(None, None))

print("Proposed approach")
def func_gen(x):
    return lambda sent_index, word_indices: x

functions = [func_gen(cond) for cond in conds]
for function in functions:
    print(function(None, None))

print("Proposed approach 2")
# For all conditions, create a 2-argument functions that returns the condition regardless of input
functions = map(lambda cond: lambda sent_index, word_indices: cond, conds)
for function in functions:
    print(function(None, None))

outputs:

Current approach
e
e
e
e
e
e
e
e
Proposed approach
i
t
e
r
a
b
l
e
Proposed approach 2
i
t
e
r
a
b
l
e

However, I can't properly verify this in Boxer, as I can't manage to download/install it correctly. The original source of C&C and Boxer seems to be down nowadays: svn.ask.it.usyd.edu.au/trac/candc

It seems a bit risky to merge this without verifying that it works as intended now.

@stevenbird
Copy link
Member

@asishm Are you able to confirm that the fix works please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential accidental capture of loop variable in Boxer
3 participants