Skip to content

Lambda doesn't see changes of global variables #3

@splitline

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 🤔

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