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


Groups > comp.lang.python > #20003

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

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <d@davea.name>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.004
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'pat': 0.05; 'function,': 0.07; 'imports': 0.07; 'subject:when': 0.07; 'url:peps': 0.09; 'thanks!': 0.14; 'called,': 0.16; 'loaded.': 0.16; 'namespace.': 0.16; 'subject: \n ': 0.16; 'subject:import': 0.16; 'subject:used': 0.16; 'url:pep-0008': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'cc:no real name:2**0': 0.21; 'file,': 0.21; 'header:In-Reply-To:1': 0.22; 'module,': 0.23; 'url:dev': 0.23; 'subject:use': 0.24; 'cc:2**0': 0.26; 'figure': 0.26; 'all,': 0.27; 'function': 0.27; 'import': 0.27; 'cc:addr:python.org': 0.29; 'pm,': 0.29; 'header,': 0.30; 'subject:?': 0.31; 'header :User-Agent:1': 0.33; 'too': 0.33; 'form,': 0.35; 'url:python': 0.35; 'beginning': 0.36; 'checks': 0.37; 'received:192': 0.38; 'url:org': 0.39; 'put': 0.40; 'easy': 0.60; 'believe': 0.65; 'header:Reply-To:1': 0.70; 'xxx': 0.71; 'reply-to:no real name:2**0': 0.72; '08:48': 0.84; 'lei': 0.84
Date Tue, 07 Feb 2012 21:05:15 -0500
From Dave Angel <d@davea.name>
User-Agent Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.24) Gecko/20111109 Thunderbird/3.1.16
MIME-Version 1.0
To Lei Cheng <pat.inside@gmail.com>
Subject Re: when to use import statements in the header, when to use import statements in the blocks where they are used?
References <CACUCHEBhLhCCLL3OOudBKnuD2BZJvDkYzjmoi_nsVR+JPzEENA@mail.gmail.com>
In-Reply-To <CACUCHEBhLhCCLL3OOudBKnuD2BZJvDkYzjmoi_nsVR+JPzEENA@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:2S64dxMGwB+4igY4NotuIUzU97fHPoIEQNjTXj2Twvp s5fGypCJvuhTE+GjcekHzEFOH4bbL4JyqKDzHetx+a9im//bm9 /zxgrc5UxEK7g0Qm5/54t7BcO7L4nZNOBONeaRMklKwQa3MZCm 4pBB62P7i4fqzKtzsKBkob55eAZgpzbzykMKDrIsOikkxUxVlc WXfDCObRXfd2qRvycwrm9BGLFDsg1f3SrAMsJ0w9MiEG5oB9QN dX4a1p0aRYAEe8RqTcrDJCkHtA8VV60RTLTB9ekLI4O2oRWwm/ MHaVabsSeM29RnP+2D385U4HBStdZCbFO/eeui5RdkC6q71rNl MUGEB2vt2SCRIozUtRXY=
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
Reply-To d@davea.name
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5529.1328666724.27778.python-list@python.org> (permalink)
Lines 29
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328666724 news.xs4all.nl 6949 [2001:888:2000:d::a6]:42843
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20003

Show key headers only | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web