Python global variable

Don Lun picture Don Lun · Apr 4, 2011 · Viewed 34.1k times · Source
def say_boo_twice():
  global boo
  boo = 'Boo!'
  print boo, boo

boo = 'boo boo'
say_boo_twice()

The output is

Boo! Boo!

Not as I expected. Since I declared boo as global, why is the output not:

boo boo boo boo

Answer

Dr McKay picture Dr McKay · Apr 4, 2011

You've changed boo inside your function, why wouldn't it change? Also, global variables are bad.