I tried to create a variable with spaces in the name, and I came up with this:
>>> classic_var = 'spam'
>>> locals()['classic_var']
'spam'
>>> classic_var
'spam'
>>> locals()['local_var'] = 'eggs'
>>> locals()['local_var']
'eggs'
>>> local_var
'eggs'
>>> locals()['variable with space in names'] = 'parrot'
>>> locals()['variable with space in names']
'parrot'
But someone replied to that (source):
The dictionary returned by locals() just represents the entries in the local symbol table, these are not the symbols themselves. So changing this dictionary does not create any variable at all. See here: https://docs.python.org/3/library/functions.html#locals
So I wonder why does this works:
>>> a = 'test'
>>> locals()['a'] = 'hello'
>>> locals()['b'] = 'world'
>>> print(a, b)
hello world
Inside a function, locals modification doesn't works, but with globals(), same behavior.
The documentation says: "changes may not affect the values of local and free variables used by the interpreter". "may". But what is the condition? why it "may"? Under what circumstance?
This isn't for professionnal project, just research about how python works and how we can tweak things to create weird things.