An imported module doesn't have access to names in the module that imports it. It only has access to names it defines and names it imports. One way around this is to poke x into module foo like this:
>>> import foo
Traceback (innermost last):
File "<console>", line 1, in ?
File "D:\Projects\TickTOC\src\python\foo.py", line 2, in simple
>>> foo.x = 10
You could copy over a list of variables like this:
>>> for var in ['y', 'z']:
... setattr(foo, var, locals()[var])
You could also have foo grab the variables from sys.modules['__main__']:
>>> import sys
But I would consider any of these solutions only as a last resort. One alternative would be to put the shared variables in another module that foo can import. Or you could pass a parameter to simple().
> From: Satya Ghattu <sghattu@...>
> Date: 2004/05/13 Thu PM 06:11:35 EDT
> To: jython-users@...
> Subject: [Jython-users] simple jython question
> In the interpreter lets say I defined a variable, and I would like to
> refer this variable from another module that I import. For example I
> have module foo,
> def simple():
> print x
> >>> import foo
> >>> x = 10
> >>> foo.simple()
> Traceback (innermost last):
> File "<console>", line 1, in ?
> File "C:\scripts\foo.py", line 2, in simple
> NameError: x
> what am i missing?
> This SF.Net email is sponsored by: SourceForge.net Broadband
> Sign-up now for SourceForge Broadband and get the fastest
> 6.0/768 connection for only $19.95/mo for the first 3 months!
> Jython-users mailing list