Help on object scope?

bmaron2 at hotmail.com bmaron2 at hotmail.com
Sun Feb 25 16:25:58 EST 2007


Hello everybody,

I have a (hopefully) simple question about scoping in python. I have a
program written as a package, with two files of interest. The two
files are /p.py and /lib/q.py

My file p.py looks like this:

---

from lib import q

def main():
  global r
  r = q.object1()
  s = q.object2()

if __name__ == "__main__":
  main()

---

My file q.py in the subdirectory lib looks like this:

class object1:
  t = 3

class object2:
  print r.t

---

Python gives me an error, saying it can't recognize global name r.
However I define r as global in the top-level main definition! Can
anyone suggest how I can get around this, if I want to define and bind
global names inside of main() which are valid in all sub-modules?

Thanks very much for your help!




More information about the Python-list mailing list