Help on object scope?

Bart Ogryczak B.Ogryczak at gmail.com
Tue Feb 27 11:27:14 EST 2007


On Feb 25, 10:25 pm, bmar... at hotmail.com wrote:
> 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

"Globals" are only global within the module they are defined in.
Change "global r" to "import __main__; __main__.r = r", and in q.py
add "from __main__ import r".






More information about the Python-list mailing list