Module trouble [newbie]

B

Boris Ozegovic

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:
100
A printing
B is 100

??

:)
 
D

Diez B. Roggisch

Boris said:
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:

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
 
B

Boris Ozegovic

Diez said:
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
 
D

Diez B. Roggisch

Boris said:
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
 
B

Boris Ozegovic

Diez said:
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.
 
G

Goldfish

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:


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.B printing
A is 100
A printing
B is 2000
Looks okay to me.
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top