caught in the import web again

Dave Angel davea at davea.name
Tue Nov 18 20:09:53 CET 2014


Ian Kelly <ian.g.kelly at gmail.com> Wrote in message:
> On Mon, Nov 17, 2014 at 6:20 PM, Dave Angel <davea at davea.name> wrote:
>> Ian Kelly <ian.g.kelly at gmail.com> Wrote in message:
>>> On Mon, Nov 17, 2014 at 3:17 PM, Dave Angel <davea at davea.name> wrote:
>>
>>>> In a module that might get tangled in a cycle, avoid global code
>>>>  that depends on other modules.  Instead of putting such
>>>>  initialization at top level, put inside a function that gets
>>>>  called after all suspect imports are completed. (That function
>>>>  has global keywords ).
>>>
>>> If the problem is that one of those modules would import the current
>>> module, then "after all suspect imports are completed" basically means
>>> after the current module has finished importing. So what would be
>>> responsible for calling such a function?
>>>
>>
>> If one builds a set of modules that cannot avoid recursive
>>  imports, then one can ask the code that imports it to make a call
>>  after importing. Or all that is needed to avoid exposing the mess
>>  is that the top level not be part of the loops.
> 
> This would make the module less reusable though, since the first other
> module to import it in one program might still be used in another
> program but not be the first other module to import it. Or I suppose
> for simplicity it could be required that all importers call the
> initialization function, and have the function return immediately if
> it's been called previously. However, I don't think there's any way to
> do this without ending up with smelly code.
> 

The smelly code happened when the designer "can't" break the
 cycle. I understood the question to be "then what." Two-stage
 initialization is sometimes "needed" in any language and I was
 just showing it's possible in Python. I once worked on (and then
 fixed) a build system that could not complete a build from clean.
 It needed some pieces from a previous build in order to get to
 the point where it was ready to build those pieces. Recursive
 depencies at compile and link time.

-- 
DaveA




More information about the Python-list mailing list