You are not logged in.

#1 2004-12-26 16:05:27

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Python namespaces

example of my problem:

a program is made of three files, 1.py , 2.py , 3.py ,

i start the program from 1.py and use 'import' for the other two,

# 1.py
import 2, 3

my question is, how can i use stuff in 1 and 2 from 3 , i can't get it to work no matter what i try, python just complains about no such global variable, it must be possible somehow! ,
i've tried global statement but that just seems to work inside the same file and from that file containing the imports,


arch + gentoo + initng + python = enlisy

Offline

#2 2004-12-26 23:40:15

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

1 imports 3
and you want 3 to import 1

oh boy..

Offline

#3 2004-12-27 17:21:01

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: Python namespaces

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,


arch + gentoo + initng + python = enlisy

Offline

#4 2004-12-28 11:56:17

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

ok if I understand correctly now, you want some 'local' variables to be included in a seperate file because you cannot stand the one file being too long.
well If I were in your shoes (those english phrases are so funny), I would just make those variables global if this doesn't work (consider posting some code):

[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

Offline

#5 2004-12-28 17:20:24

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: Python namespaces

arrrrrgghhh!!!
i know that way zeppelin but how do i access variables the other way around,
like in my example above, i import a few files in 1.py , when i start 1.py i start a few classes in 2 and 3 , the problem is now how those classes can access variables from inside 1,


arch + gentoo + initng + python = enlisy

Offline

#6 2004-12-28 17:45:36

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,771

Re: Python namespaces

Yeah calm down .....

post some code  smile

Have you read the docs ?


Mr Green

Offline

#7 2004-12-29 16:37:27

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

xerxes2 wrote:

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

Offline

#8 2004-12-29 16:47:03

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

xerxes2 wrote:

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

Offline

#9 2004-12-29 19:24:22

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: Python namespaces

i've rearranged my code a bit and it's working but i haven't solved the initial problem yet, maybe it's not possible to do it that way,

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,


arch + gentoo + initng + python = enlisy

Offline

#10 2004-12-29 21:24:31

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

xerxes2 wrote:

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.

Offline

#11 2004-12-29 21:46:29

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: Python namespaces

i think you've been right all the time zeppelin, i did an experiment now as you said in your last post, use one file to store variables and you  can use it in all files you want using import, this way i get access to the exact same objects, my code is massive and it wouldn't make any sense to post it,

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!!!


arch + gentoo + initng + python = enlisy

Offline

#12 2004-12-30 13:15:47

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

well if you want that variable to be a global one you shouldn't make it self.bar
or else all the classes that you want to use this var, should be childs of class Foo (the class in 1.py)

do in 1.py

global bar


and then
bar = someobject()

Offline

#13 2004-12-30 13:49:46

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Python namespaces

.... isnt it as simple as....


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

Offline

#14 2005-02-03 13:02:40

vegai
Developer/TU
Registered: 2004-05-19
Posts: 41
Website

Re: Python namespaces

You probably want to use "from foo import bar", which means that it imports bar from module foo into current namespace, with the name foo.bar.

Offline

#15 2005-02-03 15:24:37

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Python namespaces

hey xerxes....
I don't think people really understand your issue (and I don't claim to either) - but I will say this:
If you have a design that won't work due to the rules, then the design may be flawed... there has to be a different way to accomplish the same result within the rules of python...

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

Offline

#16 2005-02-04 20:48:59

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Python namespaces

This may (or may not) be helpful:

==> 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

Offline

#17 2005-02-05 15:07:23

zeppelin
Member
From: Athens, Greece
Registered: 2004-03-05
Posts: 807
Website

Re: Python namespaces

wow alterkacker show this code to a normal person ( tongue ) and he'll go back to C

Offline

#18 2005-02-05 19:28:34

alterkacker
Member
From: Peoples Republic of Boulder
Registered: 2005-01-08
Posts: 52

Re: Python namespaces

Normal people? Sorry, don't believe I know any. And if I did I certainly wouldn't expect to find them here.

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.

Offline

Board footer

Powered by FluxBB