question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issue accessing global variables

See original GitHub issue

Hi 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:closed
  • Created 6 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
PierreQuentelcommented, Jun 28, 2019

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 :

<script type="text/python">
from browser import alert, window

def function_a():
  alert("function a")

def function_b():
  function_a()
  alert("function b")

window.function_b = function_b
</script>

<script type="text/python">
from browser import window

window.function_b()
</script>
0reactions
darthglowballcommented, Jun 27, 2019

I can’t acces global functions from one script in other scripts. I have two scripts in the main html body:

<script type="text/python">
  from browser import alert

  def function_a():
      alert("function a")
  
  global function_b
  def function_b():
      function_a()
      alert("function b")
      
</script>
<script type="text/python">
  function_b()
</script>

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found