variable scope?

deadmeat root at [127.0.0.1]
Mon Dec 25 20:00:56 EST 2000


This doesn't work:

---
import time, string

month = "2"
year = "2000"

def foo():
   if string.atoi(month) <= 3:
      year = `string.atoi(year) - 1`
      print year

foo()
---

reporting:

Traceback (most recent call last):
  File "pfft.py", line 11, in ?
    foo()
  File "pfft.py", line 8, in foo
    year = `string.atoi(year) - 1`
UnboundLocalError: Local variable 'year' referenced before assignment

but this does

---
import time, string

month = "2"
year = "2000"

def foo():
   year = "2000"
   if string.atoi(month) <= 3:
      year = `string.atoi(year) - 1`
      print year

foo()
---

how do I force foo() to use the global 'year' variable?






More information about the Python-list mailing list