Anyhow, I don't think it's quite that bad. The 'locals()' function returns the dictionary (in the pythonic sense) of local variables in module m1, and module m2 can then use that dictionary to refer back to those variables. In fact, routine 'importme()' could even do something like:
mod_locals['x'] += 1
to alter the value of variable 'x' in module m1. From the googling I've done, this is a perfectly kosher technique. I've even tried adding a variable to m1's namespace with something like:
mod_locals['newvar'] = 999
in importme(); though one source I found said not to do that for reasons I don't remember.
==> m1.py <==
import m2
print 'in m1'
x = 99
m2.importme(locals())
print 'from m1, m2.x=',m2.x
m2.showme()
==> m2.py <==
x = 42
print 'in m2'
def importme(mod_locals):
global m1x
m1x = mod_locals['x']
def showme():
print 'from m2, m1x=',m1x
And here's the output from running m1.py:
in m2
in m1
from m1, m2.x= 42
from m2, m1x= 99
perhaps making a separate class simply to contain some passed around variables?
# 4.py
class Vars:
__init__():
self.someVar = ''
self.anotherVar = []
self.bar = someobject()
# 1.py
import 3
from 4 import *
class Foo:
__init__():
if(Vars.someVar == '') Vars.someVar = 2.Foo2()
self.bar = Vars.bar
Now keep in mind, I'm not a python god... but from what I can gather, that would work.... maybe
]]>import 2, 3
print 2.variablename
print 3.anothervariable
at least, thats what I've done with my applications. btw, 123 isnt really great naming. and im not sure if you're allowed to.
iphitus
]]>do in 1.py
global bar
and then
bar = someobject()
my initial question is probably not possible to solve but i can explain it one more time,
#1.py
import 3
class Foo:
__init__():
#here i start a class from 3
MyClass = 3.Foo2()
#here is a variable that i want to use as a global
self.bar = someobject()
in my case the class from 3 is gtk stuff,
when i use that i want to use self.bar from 1 but maybe it's not possible,
my question is stupid from the beginning!!!
I don't wan't to import 1 to 3, i just want access to a few objects in 1 from 3, not the code,
so your main code is in 3.py and you need some vars (or other staff from 1.py)
in 3.py
from 1 import *
of course tha implies that in 1.py you don't import 3! (not only it won't work, but also won't help you write good code). It remains a mystery that you don't post some code so all could understand better. At least it seems now we(I) understand that your main file is 3.py and you want stuff from 1. import those stuff and you're done
if it doesn't work, and you don't want to make your code public, pm me with a link to your code, so I can help you ON THE EXACT CODE.
]]>Edit: i don't wan't to import 1 to 3, i just want access to a few objects in 1 from 3, not the code,
]]>hmm, no i don't want to import 1 from 3, i just want acces to variables from it, it works when the classes are in the same file but it doesn't work if i split the files up in minor ones,
the files just get so big and that makes it a hell to code in them,
well me again, here I understand differently.
so 1.py has
import 3
but you also want 3.py to use 1?
THE ONLY THE WAY for 3 to use 1 is to import it
and then one file imports the other which import the first (that's why I said oh boy)
if you really have this problem, then
1.py *should* just keep (store) variables/settings or whatever and NOT import 3
then 3.py can easily import 1 and use it
I believe this is what you wanted. if not, well I could be an idiot, I let others judge
]]>when i start 1.py i start a few classes in 2 and 3,
Listen, I want to help you, I don't understand this setence though. Either post some of you code (or a link to it), or I hope someone understands you better.
The only thing I 've understand is that (in 1.py) you import 2, 3
and then that you have some classes in 2.py and 3.py and that those classes want to use variables from the 1.py.
again if you don't post the structure of 2.py and 3.py all you're gonna say is aaaaaaaarghhh!
1.py
a=1
2.py
from ga import *
class yeah:
print a
now if 1.py has those variables in a class then you should make class yeah a subclass of the class in 1.py
if you have those variables in 1.py in a class and you also have them private, you need to write in the the class of 1.py a getter (and maybe a setter) too.
I know, I confused you on purpose because of your 'argh'. Post some code of 2.py and 3.py if the above don't work.
Also read http://freebooks.by.ru/view/RedHatLinux … l6u351.htm
ps. I'm a greek and xerxes was beaten the hell out by my (ancient) ancenstors. So you can keep saying 'aaaaaargh' , I just want to thank Mr. Green
]]>post some code
Have you read the docs ?
]]>[nk@Freud tmp]€ cat fo.py
a='1'
[nk@Freud tmp]€ cat gab.py
from fo import *
print a
[nk@Freud tmp]€ python gab.py
1