17

Python offers the function globals() to access a dictionary of all global variables. Why is that a function and not a variable? The following works:

g = globals()
g["foo"] = "bar"
print foo # Works and outputs "bar"

What is the rationale behind hiding globals in a function? And is it better to call it only once and store a reference somewhere or should I call it each time I need it?

IMHO, this is not a duplicate of Reason for globals() in Python?, because I'm not asking why globals() exist but rather why it must be a function (instead of a variable __globals__).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rolf Kreibaum
  • 273
  • 1
  • 7
  • 1
    You should rarely need to access globals at all outside of debugging, this is related http://stackoverflow.com/questions/12693606/reason-for-globals-in-python – Padraic Cunningham Jun 20 '15 at 21:11
  • I guess that if it was a variable, then it would be stored in the virtual memory, using useful space. Being a function, it allows you to store yourself that information when you actually need it (and not always). I guess that the function goes and search for the global variables. So if you don't call it, no extra memory is used... I'm not sure about all this, it is just my supposition. – tomasyany Jun 20 '15 at 21:11
  • I think answering this question will require specific historical knowledge, but given that `locals` has to be a function (it needs to update the dict when called), I suspect `globals` was made a function for symmetry with `locals`. – user2357112 Jun 20 '15 at 21:15
  • See this answer: http://stackoverflow.com/a/12694065/3516028. Not exactly what you are looking for, but actually when you think that the globals variables can change, then it is better to have a function that collects them all when you search for them, than having a variable that the interpreter must update himself. – tomasyany Jun 20 '15 at 21:16
  • 1
    @PadraicCunningham: I read that thread as well. My particular reason is that I want to work in a REPL environment. And using ipython -i workspace.py gives me a working python shell. I need to load some global variables from files though, that's where globals() comes into play. – Rolf Kreibaum Jun 20 '15 at 21:19

1 Answers1

17

Because it may depend on the Python implementation how much work it is to build that dictionary.

In CPython, globals are kept in just another mapping, and calling the globals() function returns a reference to that mapping. But other Python implementations are free to create a separate dictionary for the object, as needed, on demand.

This mirrors the locals() function, which in CPython has to create a dictionary on demand because locals are normally stored in an array (local names are translated to array access in CPython bytecode).

So you'd call globals() when you need access to the mapping of global names. Storing a reference to that mapping works in CPython, but don't count on other this in other implementations.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343