Issue accessing global variables
See original GitHub issueHi all. The code below demonstrates a bug in accessing global variables. As of 29-Sep-2017, this bug is produced when run with this brython version: https://cdn.rawgit.com/brython-dev/brython/stable/www/src/brython.js But it is not produced with this brython version: https://cdnjs.cloudflare.com/ajax/libs/brython/3.3.20/brython.js
It should print 42 all 4 times, but in the buggy version, the first (straightforward) case fails, claiming the global variable is not defined:
`
def setSomeGlobal():
global someGlobal
someGlobal = 42
setSomeGlobal()
def getSomeGlobal():
return someGlobal
# Directly accessing the global will fail:
try: print('someGlobal -->', someGlobal) # Error: name 'someGlobal' is not defined
except Exception as e: print('Error:', e)
# But "indirectly" accessing the global succeeds:
print('globals()["someGlobal"] --> ', globals()["someGlobal"]) # 42 (ok)
print('getSomeGlobal() --> ', getSomeGlobal()) # 42 (ok)
print('(lambda:someGlobal)() --> ', (lambda:someGlobal)()) # 42 (ok)`
Thanks!
David
Issue Analytics
- State:
- Created 6 years ago
- Comments:12 (8 by maintainers)
Top Results From Across the Web
Troubleshooting global variable problems - IBM
Having READ permission is all that is needed to know what the value of the global variable is by issuing a VALUES(Global Variable...
Read more >Why Is Using Global Variables Considered a Bad Practice?
The major problem with them is that any change in their values is propagated to the entire program. Such a change is often...
Read more >Global Variables – Still a Major Source of Problems in IT ...
For a variable to be global, it must be accessed in a way not involving the inputs to a function. There are, however,...
Read more >What are the problems with global variables? - Quora
Global variables can easily conflict with the work of other programmers. For example, a global variable named "total" could easily be used by...
Read more >How to Access Global Variable if there is a Local Variable with ...
We can access global variable if there is a local variable with same name in C and C++ through Extern and Scope resolution...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Like in CPython, the scripts are run each in its own namespace ; sharing the same namespace for all scripts would cause terrible bugs.
To share variables between scripts you can use the object
browser.window
:I can’t acces global functions from one script in other scripts. I have two scripts in the main html body:
In Chrome the error is:
Traceback (most recent call last): NameError: name 'function_b' is not defined brython.js:5028 Uncaught Error at Object._b_.NameError.$factory (eval at $make_exc (brython.js:7382), <anonymous>:101:327) at Object.$B.$global_search (brython.js:5690) at eval (eval at $B.loop (brython.js:5012), <anonymous>:11:17) at $B.loop (brython.js:5012) at $B.loop (brython.js:5017) at $B.inImported (brython.js:5002) at $B.loop (brython.js:5018) at IDBOpenDBRequest.idb_cx.onsuccess (brython.js:4982)
The globals are not found in the second script’s globals(). They aren’t in browser.window either (would that be a good place for Python globals?) A workaround is by putting your globals in
__BRYTHON__
(I haven’t had any name clashes with internal Brython variables, so it seems safe?). Still really annoying though to do it like that.