circular imports

Eric Lee Green eric at badtux.org
Wed May 23 02:08:36 EDT 2001


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 22 May 2001 21:11:49 -0700, e toffi <etoffi at bigfoot.com> wrote:
>i have two modules which are interdependent, and another one which depends
> on both of them.
>what can be recommended to eliminate circular references?

1. Re-factor. That is, move the interdependent functions to one module or
the other, but not both. This will usually solve the problem. Usually if
I find that this is a problem, it means I need to create another class and
pass an instantiated object whose methods the second module calls, rather
than have the second module directly call functions in the first module.

2. If there's one function that *MUST* do something with a module that
already imported its parent module, do the import inside that one
function rather than in the global scope. This is will cause you to
lose karma rapidly, but sometimes is necessary.

3. Sometimes re-arranging the imports will help.  For example, I once
had a database class which used a user class instance to decide
whether a given person had permission to read or write a particular
record. The user information was stored in (tada!) the database, and
the user class inherited (tada!) the database class for its methods
for reading and writing user info. This got a bit hairy! What I had to
do was do "import user" as the *last* statement in the 'dbaccess'
module. Otherwise things went kapow quickly :-(.


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.5 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE7C1LT3DrrK1kMA04RAmbUAJ45Z18bMgb0LvaBejAVppSrsCExmQCg00c+
576pMo0xO8QIK999saeFmQM=
=gFHZ
-----END PGP SIGNATURE-----



More information about the Python-list mailing list