Skip to content

Issue with Variable Scope in Python within Generator Expressions #388

@melv33n

Description

@melv33n

I am facing an issue in Python where variables within generator expressions (such as all(...) or list comprehensions) are not accessible unless declared globally. For instance, in all(row[column] == fieldData for row in eventsList), both column and eventsList are not accessible in that context, resulting in the exception: name 'eventsList' is not defined. Does anyone know how to solve this without declaring the variables globally?

This won't work:

column = 'some_column'
fieldData = 'expected_value'
eventsList = [{'some_column': 'expected_value'}, {'some_column': 'other_value'}]

if all(row[column] == fieldData for row in eventsList):
    print("All rows match")
else:
    print("Not all rows match")

but will do if I append global column, fieldData, eventsList at the beggining

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions