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

Scope issue only exists in console #9140

Closed
theherk opened this issue Jan 18, 2016 · 2 comments
Closed

Scope issue only exists in console #9140

theherk opened this issue Jan 18, 2016 · 2 comments
Milestone

Comments

@theherk
Copy link

theherk commented Jan 18, 2016

Usually I correctly get the scope where I break to console in IPython with

import IPython; IPython.embed()

However, I have discovered a condition where something appears not to work properly. Take the following code for example:

class AClass(object):
    def do_thing(self, x):
        return True if 'ee' in x else False

def funky():
    obj = AClass()
    stuff = ['aye', 'bee', 'cee']
    ees = [item for item in stuff if obj.do_thing(item) is True]
    import IPython; IPython.embed()

if __name__ == '__main__':
    funky()

Once you get to the console here, ees is correctly set. However, if you attempt:

ees2 = [item for item in stuff if obj.do_thing(x) == True]

you get

NameError: name 'obj' is not defined

Of course it is defined. I just used it in the same scope. Am I missing something here?

@takluyver
Copy link
Member

See this comment for an explanation, but in brief, your list comprehension creates a closure over obj, and closures don't play nicely with dynamic evaluation (which is what embed does).

This has been a bug in embed() for years, and we haven't managed to find a solution. It seems to be a limitation of Python itself. I'll have another think, but don't hold your breath.

Closing as a duplicate of #136, but feel free to keep using this thread if you want any more clarification.

@theherk
Copy link
Author

theherk commented Jan 19, 2016

Oh, I see; fascinating. Thanks for looking into it.

@Carreau Carreau added this to the no action milestone Jan 27, 2016
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

3 participants