New submission from Dominic Littlewood <11dlittlew...@gmail.com>:

In PEP 8, it is stated that:
"Imports are always put at the top of the file, just after any module comments 
and docstrings, and before module globals and constants."

Note the word "always". This advice makes sense because it cuts down on 
performance costs.

I recently got into an argument about this, and so I created a script to find 
all the times in the standard library that an import statement was used inside 
a function or method. I was expecting to get one or two, but it's safe to say 
that the answer 1576 was a little surprising. (I was on 3.7.4, if anyone wants 
to check.)

It turns out that more than one in five modules (defined as any .py file) 
contain at least one violation of this rule. In the standard library, there are 
more violations of the rule than there are while loops. Do we need to either a) 
fix large parts of the standard library or b) rewrite PEP 8 to say that this 
might not actually be all that bad?

----------
components: Library (Lib)
messages: 353385
nosy: plokmijnuhby
priority: normal
severity: normal
status: open
title: Imports at top of module is often not used
type: performance

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue38297>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to