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

Functions defined in PythonREPL aren't accessible to other functions due to Locals != Globals #4

Open
nasirus opened this issue Apr 9, 2023 · 1 comment

Comments

@nasirus
Copy link
Owner

nasirus commented Apr 9, 2023

See this replit for a demonstration of what the problem seems to be
https://replit.com/@XidaRen/Test-Exec-Problem#main.py

See this stack overflow question for my documentation of my explorations
https://stackoverflow.com/questions/75970939/python-exec-fails-to-define-function-when-locals-and-globals-are-different

It seems that Exec can't have different Locals and Globals or else using function definitions would fail.

Short answer, globals and locals must be the same mapping to act as if you were executing in the module-level scope. If you pass two different mappings, it is executed like a class definition. Just as if you define a = classvar in a class block, methods won't have access to a
https://stackoverflow.com/questions/46354011/python-variable-defined-by-exec-not-defined-in-a-function-scope
(comment by juanpa.arrivillaga)

Symptoms:
Please copy and paste this code into a Python environment to use it.
Observation: name 'is_prime' is not defined
Thought:My apologies for the confusion. Here is a Python function that should give you the sum of the first N prime numbers:

{
"action": "Python REPL",
"action_input": "def is_prime(n):\n if n <= 1:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True\n\ndef sum_first_n_primes(n):\n count = 0\n num = 2\n total = 0\n while count < n:\n if is_prime(num):\n total += num\n count += 1\n num += 1\n return total\n\nprint(sum_first_n_primes(10)) # Example usage"
}
Please copy and paste this code into a Python environment to use it.
Observation: name 'is_prime' is not defined
Thought:My apologies for the confusion. Here is a Python function that should give you the sum of the first N prime numbers:

{
"action": "Python REPL",
"action_input": "def is_prime(n):\n if n <= 1:\n return False\n for i in range(2, int(n**0.5) + 1):\n if n % i == 0:\n return False\n return True\n\ndef sum_first_n_primes(n):\n count = 0\n num = 2\n total = 0\n while count < n:\n if is_prime(num):\n total += num\n count += 1\n num += 1\n return total\n\nprint(sum_first_n_primes(10)) # Example usage"
}
Please copy and paste this code into a Python environment to use it.
Observation: name 'is_prime' is not defined

@nasirus
Copy link
Owner Author

nasirus commented Apr 9, 2023

Based on the information provided, it appears that the issue is related to the fact that when using the Python REPL, the locals and globals must be the same mapping in order for functions to be accessible to other functions. This is because when two different mappings are passed, it is executed like a class definition, and functions will not have access to variables defined in the class block.

To resolve this issue, you can ensure that the locals and globals are the same mapping when using the Python REPL. You can do this by passing the same mapping to both the locals and globals parameters when calling the exec() function.

For more information, please see the following resources:

I hope this helps! Please let me know if you have any further questions.

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