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

Weird scope issues #5

Open
serendipiddy opened this issue Sep 14, 2020 · 0 comments
Open

Weird scope issues #5

serendipiddy opened this issue Sep 14, 2020 · 0 comments

Comments

@serendipiddy
Copy link

serendipiddy commented Sep 14, 2020

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.

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

No branches or pull requests

1 participant