Import question

N

ncf

In file A, I have an instance of a class and then I import file B
(import fileB as fb). In file B, I need to access file A's class
instance. Is there anyway I can do this? (I hope that was descriptive
enough :\)

-Wes
 
L

Lonnie Princehouse

Circular import issues can usually be resolved by moving import
statements into the bodies of functions which aren't executed when the
module itself is imported. Simple example:

---- fileA.py ------

import fileB as fb
foo = 10 # we're going to access foo from fileB

fb.do_something_with_foo()

---- fileB.py ------

def do_something_with_foo():
import fileA as fa # import is INSIDE the function
print "foo is ", fa.foo

----------------------
 
P

Paul McNett

ncf said:
In file A, I have an instance of a class and then I import file B
(import fileB as fb). In file B, I need to access file A's class
instance. Is there anyway I can do this? (I hope that was descriptive
enough :\)

Let's see...

# -- fileA.py
class Test(object): pass

myInstance = Test()

import fileB as fb
fb.myInstance = myInstance
fb.test()

# -- fileB.py
myInstance = None

def test():
print "myInstance is %s" % myInstance

if __name__ == "__main__":
test()

# -- now to the command line to test:
pmcnett@sol:~/projects/dabo/dabo/ui/uiwx $ python fileB.py
myInstance is None
pmcnett@sol:~/projects/dabo/dabo/ui/uiwx $ python fileA.py
myInstance is <__main__.Test object at 0xb7dfcf6c>

Is this what you want? BTW, what you call "files", we refer to as
"scripts" (if run) or "modules" (if imported).
 
N

ncf

Hmm...thanks for the replies. Judging by this, it looks like I might
still be in a slight perdiciment with doing it all, but time will tell.
I wish there were a way I could reference across multiple modules.

Well, thanks for your help. Hopefully I'll be able to work out some
*simple* solution for all of this.

-Wes
 
C

Christopher Subich

ncf said:
Hmm...thanks for the replies. Judging by this, it looks like I might
still be in a slight perdiciment with doing it all, but time will tell.
I wish there were a way I could reference across multiple modules.

Well, thanks for your help. Hopefully I'll be able to work out some
*simple* solution for all of this.

What exactly is it that you're trying to do? You don't need access to
the class definition (which would be in a module) if you just want to
manipulate a particular _instance_. Advantage of dynamic typing and all.

For example:

module1:
def function(x):
x.method(1)

module2:
class foobar:
def method(x):
print 'I received', x, ', aren't I happy?!'

import module1
obj = foobar()
module1.function(obj)
 
N

ncf

I've got multiple instances I want globally available in the secondary
modules, which can't easily be passed around with every function call
without driving me nuts, so I wish to have all variables from the Main
module passed to each of the other modules it creates.

One such example would be in Main, we call the SettingsParse() function
or whatever, which creates a Settings class storing all settings. This
class needs to be readily accessible by extensions (dynamically loaded)
and almost all other modules in the project.

-Wes
 
N

ncf

Crap. Forgot to mention that in some instances, I do want the class
definitions to create new instances and such. Sorry :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,311
Members
47,986
Latest member
ColbyG935

Latest Threads

Top