when to use import statements in the header, when to use import statements in the blocks where they are used?
pat.inside at gmail.com
Wed Feb 8 03:38:08 CET 2012
On Wed, Feb 8, 2012 at 10:05 AM, Dave Angel <d at davea.name> wrote:
> On 02/07/2012 08:48 PM, Lei Cheng wrote:
>> Hi all,
>> In a py file, when to use import statements in the header, when to use
>> import statements in the blocks where they are used?
>> What are the best practices?
>> Best practice is to put all the imports at the beginning of the module,
> so they are easy to spot.
> If you put an import inside a function, it gets re-executed each time the
> function is called, which is a waste of time. Not too much, since import
> first checks sys.modules to see if it's already loaded.
> Also, avoid the from xxx import * form, as it pollutes the
> namespace. And it makes it hard to figure out where a particular name is
> I believe these and other best practices can be found in pep8.
yeah, I read pep8.
However I find in the file path/to/djcelery/loaders.py from django-celery
source, there are so many import/from statements used inside functions, I
do not know why the author coded like this. Are there any special facts?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list