python-apps icon indicating copy to clipboard operation
python-apps copied to clipboard

Issue with Variable Scope in Python within Generator Expressions

Open melv33n opened this issue 1 year ago • 3 comments

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

melv33n avatar Dec 09 '24 11:12 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

Hey!

Is this in the Execute Python function, Liquid or with a custom app?

It indeed must have something to do with local vs global scopes if the execute python function 🤔

frikky avatar Dec 12 '24 23:12 frikky

Is this in the Execute Python function, Liquid or with a custom app?

It's in Execute Python Function inside Shuffle-tools app

melv33n avatar Dec 13 '24 12:12 melv33n

@0x0elliot can you look into this? Very relevant to the code editor work

frikky avatar Dec 17 '24 10:12 frikky