On 17/05/2011, at 18:56 , Benoît HERVIER wrote: > 2011/2/14 A.T.Hofkamp <[email protected]>: >> Arve Knudsen wrote: >>> >>> Hi >>> >>> I've found that pylint 0.23.0 will always issue a warning that a reimport >>> takes place when I >> >> import a module within a function, even if the module is only imported in >> that one place. I've >> verified that pylint 0.21.0 on the other hand does not issue any warning in >> this case. Is this valid >> new behaviour in pylint, that importing within functions should be warned >> against?? >> >> I would say yes. >> In python it is recommended (for as long as I can remember) to import >> modules only at the top of the file. It makes it easier to find the imports, >> and it limits import overhead to just during start up. >> >> There are a few exceptions to this rule, the most important being import of >> a module that you almost never need which takes a lot of time to start. >> I have yet to encounter that case though. >> >> Albert
* I think there are two issues. * The first is that the warning is inaccurate. It refers to the reimport of a module that has not been imported before at all. So in that sense the warning is "wrong". Ideally it should be a warning against a "local import", then it could be correctly disabled. * The second is as to whether local imports are poor design. There are several good arguments *for* local imports: - if an imported module is little used, they don't pollute global scope - they "declare" the use of a module right near where it is actually used. - it makes it easier to refactor/move code between modules as you take the "dependents" with you when you cut and paste. - local imports are generally not expensive whatsoever. * In contrast, global imports - pollute module scope - declare dependencies far from their actual usepoint - this then encourages the importing of modules that are in fact no longer used - inhibit refactoring into new modules as the movement of code requires active pylinting to discover the imports that need to be added to the new module * Note that the standard library uses local imports too in places ... * Of course there are many pylint decisions regarding "good style" ... however if pylint could generate a correct warning then users could individually disable it. Derek. _______________________________________________ Python-Projects mailing list [email protected] http://lists.logilab.org/mailman/listinfo/python-projects
