|
|
 |
 |
 |
 |
Python Programming Language
|
 |
 |
 |
 |
 |
 |
 |
 |
Module trouble [newbie]
Can somebody explaint this to me: I have module a.py A = 100 import b print "A printing" print "B is %s" % b.B and module b.py B = 2000 import a print "B printing" print "A is %s" % a.A I thought that output would be: B printing A is 100 A printing B is 2000 Because import b would execute b.py, and in b.py line "import a" would be ignored, but my output is: >>> import a
100 A printing B is 100 ?? :) -- http://www.nacional.hr/articles/view/23894/23
Boris Ozegovic wrote: > Can somebody explaint this to me: > I have module a.py > A = 100 > import b > print "A printing" > print "B is %s" % b.B > and module b.py > B = 2000 > import a > print "B printing" > print "A is %s" % a.A > I thought that output would be: > B printing > A is 100 > A printing > B is 2000 > Because import b would execute b.py, and in b.py line "import a" would be > ignored, but my output is: >>>> import a > 100 > A printing > B is 100
Are you sure the above is what you really used for your test? Because your output features a single 100, which the above lacks a print-statement for. Diez
Diez B. Roggisch wrote: > Are you sure the above is what you really used for your test? Because your > output features a single 100, which the above lacks a print-statement for.
Yeah, I cancelled the message, but synchronization allready happened. :) Problem was in some other place. One more question: is calling import inside function definition poor design? e.g. def foo(): doSomething: import someModule someModule.doSomethin -- http://www.nacional.hr/articles/view/23894/23
Boris Ozegovic wrote: > Diez B. Roggisch wrote: >> Are you sure the above is what you really used for your test? Because >> your output features a single 100, which the above lacks a >> print-statement for. > Yeah, I cancelled the message, but synchronization allready happened. :) > Problem was in some other place. > One more question: is calling import inside function definition poor > design? e.g. > def foo(): > doSomething: > import someModule > someModule.doSomethin
I use it sometimes myself, to avoid otherwise circular imports. However, I don't like it very much. Try to go without it I'd say, but that is just a gut-feeling. Diez
Diez B. Roggisch wrote: > I use it sometimes myself, to avoid otherwise circular imports.
Circular imports are the reason why I have module issues. Last question: if I put modules in some package and then try to import one I get "AttributeError: 'module' object has no attribute 'a' Code is: a.py A = 756 import someFolder.b as b print "A printing" print "B is %s" % b.B b.py B = 2000 import someFolder.a as a print "B printing" print "A is %s" % a.A How can I do circular imports if I use packages? Darn. I don't remeber this module gothcas when reading Learning Python. -- http://www.nacional.hr/articles/view/23894/23
On Feb 23, 6:44 am, Boris Ozegovic <silovana.vjever@com.gmail> wrote:
> Can somebody explaint this to me: > I have module a.py > A = 100 > import b > print "A printing" > print "B is %s" % b.B > and module b.py > B = 2000 > import a > print "B printing" > print "A is %s" % a.A > I thought that output would be: > B printing > A is 100 > A printing > B is 2000 > Because import b would execute b.py, and in b.py line "import a" would be > ignored, but my output is: > >>> import a > 100 > A printing > B is 100 > ?? > :) > --http://www.nacional.hr/articles/view/23894/23
a.py ======= A = 100 import b print "A printing" print "B is %s" % b.B b.py ======= B = 2000 import a print "B printing" print "A is %s" % a.A Python 2.4.4 (#71, Oct 18 2006, 08:34:43) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import a
B printing A is 100 A printing B is 2000
Looks okay to me.
On Fri, 23 Feb 2007 15:12:47 +0100, Boris Ozegovic <silovana.vjever@com.gmail> declaimed the following in comp.lang.python: > How can I do circular imports if I use packages? Darn. I don't remeber > this module gothcas when reading Learning Python.
Personally -- I wouldn't try... Move the common items to a third file which both existing files then import. Instead of: a.py ===== import b A = xyz print A, b.B b.py ==== import a B = mno print B, a.A use common.py ========= A = xyz B = mno a.py ==== import common #do stuff b.py ==== import common #do stuff -- Wulfraed Dennis Lee Bieber KD6MOG wlfr@ix.netcom.com wulfr@bestiaria.com HTTP://wlfraed.home.netcom.com/ (Bestiaria Support Staff: web-a@bestiaria.com) HTTP://www.bestiaria.com/
|
 |
 |
 |
 |
|