18

I know that I can write:

foo = 'bar'
def update_foo():
  global foo
  foo = 'baz'

But do I really need two lines of code there? Python, alas, won't allow me to say

global foo = 'baz'

I could also mash the two lines together with the unfortunately repetitive

global foo; foo = 'baz'

Any other shortcuts? I'm on Python 2.6.5, but I'd be curious to hear responses for Python 3 as well.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
  • 2
    What is the intent of such exercise? When using `global` you declare you intend to meddle with variable that does not belong to you (presumably s/o else made) - but why would you override it right away, even before checking what's in it? Does not seem to make practical sense... – Nas Banov Jun 21 '10 at 23:46
  • I agree, I've always wanted to do this. I also want smarter tuple unpacking, but you can't win them all :) – Matt Joiner Jun 22 '10 at 01:24
  • @NasBanov: I almost never check what is in it before overwriting a global variable? Most of the time, this is some kind of setting like speaker sound or mouse speed, why would I need to check what the current one is before setting it to the new one the user requested? If I want to _read_ a global variable, thanks to Python's scoping, I don't even need the global keyword most of the time! – Thomas Jun 06 '19 at 09:15

5 Answers5

21

You could use my favorite alternative to global (a pretty idiosyncratic taste...):

import sys
thismodule = sys.modules[__name__]
thismodule.foo = 'bar'

def update_foo():
  thismodule.foo = 'baz'

Once you've made the thismodule reference, you don't need to use global in this module, because you're always working with qualified names rather than bare names (a much better idea IMHO... but maybe in MHO only, I've not been able to convince Guido to supply thismodule [[or some other identifier with this functionality]] back when Python 3 was gestating).

Note that the first assignment to foo, at global level, can be done either with this explicit syntax, or by assigning to barename foo as you do in your code (I guess it's not surprising that my preference goes to the explicit form, though, in this case, just barely).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Giving the module a 'this' keyword? For some reason that just gives me the heebiejeebies. If I were Guido, I'd make global declarations include a giant red flashing warning sign. – Evan Plaice Jun 22 '10 at 00:07
7

It's two statements, there aren't any other forms.

Ian Bicking
  • 9,762
  • 6
  • 33
  • 32
6

You could write it like this using the globals() dictionary:

def update_foo():
  globals()['foo'] = 'baz'

but I would just stick with the 2 lines or the separating with a ; approach.

mikej
  • 65,295
  • 17
  • 152
  • 131
5
def update_foo():
    globals().update(foo='baz')
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
2

If it makes you feel better to put it all on one line...

global foo; foo = 'baz'
Evan Plaice
  • 13,944
  • 6
  • 76
  • 94