Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20003 > unrolled thread

Re: when to use import statements in the header, when to use import statements in the blocks where they are used?

Started byDave Angel <d@davea.name>
First post2012-02-07 21:05 -0500
Last post2012-02-07 21:05 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: when to use import statements in the header, when to use import statements in the blocks where they are used? Dave Angel <d@davea.name> - 2012-02-07 21:05 -0500

#20003 — Re: when to use import statements in the header, when to use import statements in the blocks where they are used?

FromDave Angel <d@davea.name>
Date2012-02-07 21:05 -0500
SubjectRe: when to use import statements in the header, when to use import statements in the blocks where they are used?
Message-ID<mailman.5529.1328666724.27778.python-list@python.org>
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?
>     Thanks!
>
> Pat
>
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 declared.

I believe these and other best practices can be found in pep8.

http://www.python.org/dev/peps/pep-0008/

-- 

DaveA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web