Import question

Paul McNett p at ulmcnett.com
Tue Aug 9 15:26:12 EDT 2005


ncf wrote:
> 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 at sol:~/projects/dabo/dabo/ui/uiwx $ python fileB.py
myInstance is None
pmcnett at 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).

-- 
Paul McNett
http://paulmcnett.com




More information about the Python-list mailing list