Packages

B

Bill Hale

I have a question on packages.

Suppose I have a package P with modules A.py, B.py, and C.py.
Suppose that C.py defines a class X.
Assume that the package __init__.py file contains the statement
"from C import X".

If I write an external Python program H.py, then I can refer to
the class X by doing:

import P
myX = P.X()

My question is that I would like to use the same method for
consistentcy sake for the modules A and B in the package.
That is, if A needs to use the class X from C, I would like
to do the following in the file A.py:

import P
aX = P.X()

I tried this and it works. But, is there a problem with
recursion here since C might refer to A, or B might refer to
A and C refer to B?

Is this even a good idea? Is there another way to achieve
the consistency whether referring to class X from an
external Python program or referring to class X from a
sibling Python program in the package?

Of course, the normal way to refer to class X in program A.py
is to do the following:

from C import X
aX = X()

Bill Hale
 
S

Sybren Stuvel

Bill Hale enlightened us with:
I tried this and it works. But, is there a problem with recursion
here since C might refer to A, or B might refer to A and C refer to
B?

Place all classes that are shared in another package. That should
solve the circular dependencies.

Sybren
 

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,262
Messages
2,571,043
Members
48,769
Latest member
Clifft

Latest Threads

Top