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

Problem with if clause in generator expression on class level #46470

Closed
Cito mannequin opened this issue Mar 2, 2008 · 6 comments
Closed

Problem with if clause in generator expression on class level #46470

Cito mannequin opened this issue Mar 2, 2008 · 6 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-mac OS-windows stdlib Python modules in the Lib dir tests Tests in the Lib/test dir topic-regex topic-tkinter topic-unicode topic-XML type-bug An unexpected behavior, bug, or error

Comments

@Cito
Copy link
Mannequin

Cito mannequin commented Mar 2, 2008

BPO 2217
Nosy @birkenfeld, @Cito

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2008-03-02.17:56:45.131>
created_at = <Date 2008-03-02.17:43:30.918>
labels = ['expert-regex', 'OS-mac', 'expert-XML', 'tests', 'expert-unicode', 'interpreter-core', 'type-bug', 'expert-tkinter', 'library', 'OS-windows']
title = 'Problem with if clause in generator expression on class level'
updated_at = <Date 2008-03-03.21:32:40.302>
user = 'https://github.com/Cito'

bugs.python.org fields:

activity = <Date 2008-03-03.21:32:40.302>
actor = 'georg.brandl'
assignee = 'none'
closed = True
closed_date = <Date 2008-03-02.17:56:45.131>
closer = 'georg.brandl'
components = ['Interpreter Core', 'Library (Lib)', 'macOS', 'Regular Expressions', 'Tests', 'Tkinter', 'Unicode', 'Windows', 'XML']
creation = <Date 2008-03-02.17:43:30.918>
creator = 'cito'
dependencies = []
files = []
hgrepos = []
issue_num = 2217
keywords = []
message_count = 6.0
messages = ['63182', '63183', '63184', '63187', '63189', '63234']
nosy_count = 2.0
nosy_names = ['georg.brandl', 'cito']
pr_nums = []
priority = 'normal'
resolution = 'wont fix'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue2217'
versions = ['Python 2.5']

@Cito
Copy link
Mannequin Author

Cito mannequin commented Mar 2, 2008

The following code throws a NameError which seems to be a bug existing
since Python 2.4 up to the current 2.5.2.

class A:
    a = 'test'
    [c for c in a]
    (c for c in a)
    tuple(c for c in a)
    [c for c in a if c in a]
    (c for c in a if c in a)
    tuple(c for c in a if c in a) # --> NameError

@Cito Cito mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) stdlib Python modules in the Lib dir OS-mac topic-regex tests Tests in the Lib/test dir topic-tkinter topic-unicode OS-windows topic-XML type-bug An unexpected behavior, bug, or error labels Mar 2, 2008
@birkenfeld
Copy link
Member

This may seem odd, but is correct as per spec.

The "if c in a" part is executed in a scope of its own, and class scopes
don't contribute to nested scoping.

@Cito
Copy link
Mannequin Author

Cito mannequin commented Mar 2, 2008

Thanks for the quick explanation. I understand that class scopes don't
extend and this is documented behavior.

However, the question is why the if clause is executed in a scope of its
own and where this is documented.

You would expect that the problematic generator expression is equivalent
to the following code which works fine:

class A:
    a = 'test'
    def __g(a):
        for c in a:
            if c in a:
                yield c
    tuple(__g(a))

@birkenfeld
Copy link
Member

The actual equivalent would be

class A:
    a = 'test'
    def __g(_x):
        for c in _x:
            if c in a:
                yield c
    tuple(__g(a))

i.e. the outmost iterator is evaluated in the enclosing scope; not the
if clause is in its own scope, but the whole genexp.

@Cito
Copy link
Mannequin Author

Cito mannequin commented Mar 2, 2008

Thanks, this now makes sense to me. You're right, it's rather an ugly
wart than a bug.

But I think the Python reference needs to be improved to make this clear
enough.

How about the following proposed addtions (in square brackets) to
section 5.2.5 (http://docs.python.org/ref/genexpr.html):

"Variables used in the generator expression are evaluated lazily [in the
scope of the generator function] when the next() method is called for
[the] generator object (in the same fashion as normal generators).
However, the leftmost for clause is immediately evaluated [in the
current scope] so that [an] error produced by it can be seen before any
other possible error in the code that handles the generator expression.
Subsequent for [and if] clauses cannot be evaluated immediately since
they may depend on the previous for loop."

@birkenfeld
Copy link
Member

Good idea! I've committed these changes in r61212.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) OS-mac OS-windows stdlib Python modules in the Lib dir tests Tests in the Lib/test dir topic-regex topic-tkinter topic-unicode topic-XML type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant