On Thu, Feb 17, 2011 at 1:25 AM, Marc 'BlackJack' Rintsch <[email protected]>wrote:
> 16.02.2011 23:26, Arve Knudsen <[email protected]>: > > > A linter, to be truly useful, has to straddle the line between too > > forgiving and too strict; if pylint by default were to decide that I > > am breaking good style by importing within functions, I'd see it as > > going way overboard as to its practical purpose (and perhaps > > question its place in my tool chain). It might make sense, on the > > other hand, as a non-default check (maybe custom) for those projects > > that view the practice as a problem. > > That would be all projects following the style guide for the standard > library in PEP 8 which clearly says in the `Imports` section: > > - Imports are always put at the top of the file, just after any module > comments and docstrings, and before module globals and constants. > > I think such a check has its place as default check. Putting the > imports at top makes clear which dependencies a module has. Opposed to > ``import``\s buried deep in the module that are not just harder to find > but the reader also has to think about if and when a function is called > and the dependencies really is a dependency. While this is a sound guideline, it should not be applied with religious zeal. In contrast to a purely stylistic matter such as the naming of classes/variables etc., local imports can be motivated by technical concerns. When you perform local imports, it does not only affect the program's readability, but also its *behaviour*. I will in the general case import globally at the beginning of my modules, but in certain cases I want to defer module loading, maybe depending on a user option, and delegate it to a function. Therefore, I think pylint (for instance) should leave this to the programmer's discretion, and not try to be too smart about it. It's complicated enough in my experience to define style rules that don't get too much in your way, in real life applications. Arve
_______________________________________________ Python-Projects mailing list [email protected] http://lists.logilab.org/mailman/listinfo/python-projects
