Reason for globals() in Python?

91,165

Solution 1

Python gives the programmer a large number of tools for introspecting the running environment. globals() is just one of those, and it can be very useful in a debugging session to see what objects the global scope actually contains.

The rationale behind it, I'm sure, is the same as that of using locals() to see the variables defined in a function, or using dir to see the contents of a module, or the attributes of an object.

Coming from a C++ background, I can understand that these things seem unnecessary. In a statically linked, statically typed environment, they absolutely would be. In that case, it is known at compile time exactly what variables are global, and what members an object will have, and even what names are exported by another compilation unit.

In a dynamic language, however, these things are not fixed; they can change depending on how code is imported, or even during run time. For that reason at least, having access to this sort of information in a debugger can be invaluable.

Solution 2

It's also useful when you need to call a function using function's string name. For example:

def foo():
    pass

function_name_as_string = 'foo'

globals()[function_name_as_string]() # foo(). 

Solution 3

You can pass the result of globals() and locals() to the eval, execfile and __import__ commands. Doing so creates a restricted environment for those commands to work in.

Thus, these functions exist to support other functions that benefit from being given an environment potentially different from the current context. You could, for example, call globals() then remove or add some variables before calling one of those functions.

Solution 4

globals() is useful for eval() -- if you want to evaluate some code that refers to variables in scope, those variables will either be in globals or locals.


To expand a bit, the eval() builtin function will interpret a string of Python code given to it. The signature is: eval(codeString, globals, locals), and you would use it like so:

def foo():
    x = 2
    y = eval("x + 1", globals(), locals())
    print("y=" + y) # should be 3

This works, because the interpreter gets the value of x from the locals() dict of variables. You can of course supply your own dict of variables to eval.

Solution 5

It can be useful in 'declarative python'. For instance, in the below FooDef and BarDef are classes used to define a series of data structures which are then used by some package as its input, or its configuration. This allows you a lot of flexibility in what your input is, and you don't need to write a parser.

# FooDef, BarDef are classes

Foo_one = FooDef("This one", opt1 = False, valence = 3 )
Foo_two = FooDef("The other one", valence = 6, parent = Foo_one )

namelist = []
for i in range(6):
    namelist.append("nm%03d"%i)

Foo_other = FooDef("a third one", string_list = namelist )

Bar_thing = BarDef( (Foo_one, Foo_two), method = 'depth-first')

Note that this configuration file uses a loop to build up a list of names which are part of the configuration of Foo_other. So, this configuration language comes with a very powerful 'preprocessor', with an available run-time library. In case you want to, say, find a complex log, or extract things from a zip file and base64 decode them, as part of generating your configuration (this approach is not recommended, of course, for cases where the input may be from an untrusted source...)

The package reads the configuration using something like the following:

conf_globals = {}  # make a namespace
# Give the config file the classes it needs
conf_globals['FooDef']= mypkgconfig.FooDef  # both of these are based ...
conf_globals['BarDef']= mypkgconfig.BarDef  # ... on .DefBase

fname = "user.conf"
try:
    exec open(fname) in conf_globals
except Exception:
    ...as needed...
# now find all the definitions in there
# (I'm assuming the names they are defined with are
# significant to interpreting the data; so they
# are stored under those keys here).

defs = {}
for nm,val in conf_globals.items():
    if isinstance(val,mypkgconfig.DefBase):
        defs[nm] = val

So, finally getting to the point, globals() is useful, when using such a package, if you want to mint a series of definitions procedurally:

for idx in range(20):
    varname = "Foo_%02d" % i
    globals()[varname]= FooDef("one of several", id_code = i+1, scale_ratio = 2**i)

This is equivalent to writing out

Foo_00 = FooDef("one of several", id_code = 1, scale_ratio=1)
Foo_01 = FooDef("one of several", id_code = 2, scale_ratio=2)
Foo_02 = FooDef("one of several", id_code = 3, scale_ratio=4)
... 17 more ...

An example of a package which obtains its input by gathering a bunch of definitions from a python module is PLY (Python-lex-yacc) http://www.dabeaz.com/ply/ -- in that case the objects are mostly function objects, but metadata from the function objects (their names, docstrings, and order of definition) also form part of the input. It's not such a good example for use of globals() . Also, it is imported by the 'configuration' - the latter being a normal python script -- rather than the other way around.

I've used 'declarative python' on a few projects I've worked on, and have had occasion to use globals() when writing configurations for those. You could certainly argue that this was due to a weakness in the way the configuration 'language' was designed. Use of globals() in this way doesn't produce very clear results; just results which might be easier to maintain than writing out a dozen nearly-identical statements.

You can also use it to give variables significance within the configuration file, according to their names:

# All variables above here starting with Foo_k_ are collected
# in Bar_klist
#
foo_k = [ v for k,v in globals().items() if k.startswith('Foo_k_')]
Bar_klist  = BarDef( foo_k , method = "kset")

This method could be useful for any python module that defines a lot of tables and structures, to make it easier to add items to the data, without having to maintain the references as well.

Share:
91,165
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    What is the reason of having globals() function in Python? It only returns dictionary of global variables, which are already global, so they can be used anywhere... I'm asking only out of curiosity, trying to learn python.

    def F():
        global x
        x = 1
    
    def G():
        print(globals()["x"]) #will return value of global 'x', which is 1
    
    def H():
        print(x) #will also return value of global 'x', which, also, is 1
    
    F()
    G()
    H()
    

    I can't really see the point here? Only time I would need it, was if I had local and global variables, with same name for both of them

    def F():
        global x
        x = 1
    
    def G():
        x = 5
        print(x) #5
        print(globals()["x"]) #1
    
    F()
    G()
    

    But you should never run into a problem of having two variables with same name, and needing to use them both within same scope.

  • Admin
    Admin over 11 years
    I wasn't the one downvoting, but that didn't really make sense, could you give an example code for easier understanding?
  • Richard Close
    Richard Close over 11 years
    Sure, I've expanded my answer.
  • Steven Rumbalski
    Steven Rumbalski over 11 years
    +1. Also, the dictionary returned by globals can be modified (probably a capability best left to experts). Also relevant is this quote from Dive Into Python "Using the locals and globals functions, you can get the value of arbitrary variables dynamically, providing the variable name as a string. This mirrors the functionality of the getattr function, which allows you to access arbitrary functions dynamically by providing the function name as a string."
  • Admin
    Admin over 11 years
    Not saying your answer was any better than other's, but for me it was. Thanks, I guess I need to calm down and remember that Python is not C++ :) And thanks for everyone else answering too, cleared quite alot.
  • Admin
    Admin over 11 years
    Kinda weird and seems useless for me, but maybe that's just cause I'm new to Python :P Thanks anyways
  • greggo
    greggo over 7 years
    eval("x+1") does the same thing, due to its default parameters; see help(eval)
  • greggo
    greggo over 7 years
    Changing elements in locals() usually doesn't work. Most python functions don't have a dict for locals - so when you call locals() it constructs a dict of the current values of the locals; changing that dict doesn't reflect to the locals. There were cases where functions did things like "import * from xyz" (no longer allowed) - that sets locals with names unknown at compile time; those functions were compiled with an actual locals dict, Long ago, all functions had an actual locals dict, and they have phasing out the need for that, I don't know under what conditions you have a dict now.
  • Kenan
    Kenan over 6 years
    What if the function has many parameters like, def foo(a, b, c=false). How would you pass those parameters to globals()
  • Tino
    Tino over 6 years
    @ksooklall You pass it to the function as usual: with def foo(*args): print("hw", *args) you can do: globals()['foo']() or globals()['foo']('reason', 42) etc.
  • Outcast
    Outcast over 5 years
    Have a look also here with how globals() can be used: stackoverflow.com/a/22021058/9024698.