Is it possible to define global variables in a function in Python

WoooHaaaa picture WoooHaaaa · Nov 29, 2012 · Viewed 63.8k times · Source

How do I declare a global variable in a function in Python?

That is, so that it doesn't have to be declared before but can be used outside of the function.

Answer

Jon Clements picture Jon Clements · Nov 29, 2012

Yes, but why?

def a():
    globals()['something'] = 'bob'