Skip to content

Lambda doesn't see changes of global variables #3

Description

@splitline

The globals for lambda is just a clone/snapshot of current context.

Pickora/compiler.py

Lines 355 to 358 in 7e0e8eb

func_globals = ast.Dict(keys=[], values=[])
for name in code_args[8]: # co_names
func_globals.keys.append(ast.Constant(value=name))
func_globals.values.append(ast.Name(id=name))

So if any global variables are changed after the lambda definition, the lambda won't see those changes
For example:

val = 'before'
f = lambda:val
val = 'after'
print(f())  # before

I am not sure if there are any way to fix this 🤔

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions