WedWonder: Scripts and Modules

Richard Damon Richard at Damon-Family.org
Thu Sep 12 22:38:31 EDT 2019


On 9/12/19 5:11 PM, DL Neil via Python-list wrote:
> On 12/09/19 8:22 PM, Barry Scott wrote:
>
>> In large projects with many modules import with side-effect can make
>> for a maintenance
>> burden.
>
>
> You seem to have unmasked an assumption under which I operate. (well
> done - you know what 'they' say about assumptions!)
>
> As you say, (non-designed) side-effects are undesirable.
>
> My 'rule' is that modules only contain definitions, eg classes and
> functions. Thus, *nothing* executes upon import.
>
> During a code review, some eagle-eye, noticed (and questioned) I had
> re-factored some 'constants' which control a "switch" structure from
> out of the module-space, and into the class where they 'belonged',
> without having any other 'good reason'.
>
>
> I have no recollection of the history or rationale for this
> policy/practice, nor can I remember whether it 'belongs to Python' or
> some other language from which I've imported it (hah, punny joke!)
> Perhaps it comes from my preference to from...import... rather than
> import...as... which largely eschews the module namespace?
> (or perhaps I'm just a closet control-freak?)
>
> That said, I'm sure there must be modules of my code which break this
> 'rule', somewhere (certainly in the 'quick and dirty' department).
>
Well technically, def and class are statements that are executed when
the module is imported, you need to execute them to add the definitions
into the appropriate namespace.


Perhaps the 'rule' needs to be somewhat restated to a form where the
only thing that should execute when importing a module are the
statements needed to create the definitions for the module.


-- 
Richard Damon




More information about the Python-list mailing list