From: Kent Johnson <kent37@td...> - 2004-05-13 23:25:30
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
From: Jingzhao Ou <jasonou@ya...> - 2004-05-18 06:28:16
I have the following Jython code. Class A contains an inner class B. When I try
to get access the attribute "_a" of outer class A from inner class B, I get a
I know that defining the constructor of inner class B as "def __init__(self,
outer)" should work. However, I wonder there is any better solution to this
Thanks a lot!
_a = 0
print 'in B'
print 'in A'
self.b = A.B()
From: Diez B. Roggisch <deets@we...> - 2004-05-18 09:41:25
Am Tuesday 18 May 2004 08:28 schrieb Jingzhao Ou:
> I have the following Jython code. Class A contains an inner class B. When I
> try to get access the attribute "_a" of outer class A from inner class B, I
> get a NameError error.
> I know that defining the constructor of inner class B as "def
> __init__(self, outer)" should work. However, I wonder there is any better
> solution to this problem.
No. Non-static inner classes are a syntactic sugar of java, as well as
implicit this.* when accessing methods or members. You don't have them in
python, and thus the're missing in jython.