Skip to content

Weird scope issues #5

@serendipiddy

Description

@serendipiddy

The interpreter session creates a scope which makes newly declared functions, import and variables unable to be used in lambda functions and pandas dataframe indexing.

https://stackoverflow.com/questions/35161324/how-to-make-imports-closures-work-from-ipythons-embed

With [the suggested fix], the following code should work while not working using the default embedded shell:

a=3
(lambda :a)() # default behavior: name 'a' is not defined
import time
(lambda: time.time())() # default behavior: name 'time' is not defined

(default behavior is due to a and time are not added to globals() and ipython does not make closures for local functions (the lambdas defined above) and insists to look up the variables in global scope. search closure in this page)

A permanent solution is necessary, but a temporary fix to the problem is to run globals().update(locals()) after defining any new variables before trying to use them in a lambda-function-type situation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions