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


Groups > comp.lang.python > #20014

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!feeder1.enfer-du-nord.net!tudelft.nl!txtfeed1.tudelft.nl!multikabel.net!newsfeed10.multikabel.net!xlned.com!feeder7.xlned.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'pat': 0.05; 'disagree': 0.07; 'function,': 0.07; 'imports': 0.07; 'override': 0.07; 'subject:when': 0.07; '>>>>': 0.09; 'dev': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'url:peps': 0.09; 'am,': 0.12; 'thanks!': 0.14; 'startup': 0.15; '(eg.': 0.16; 'called,': 0.16; 'discusses': 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; 'wed,': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'functions,': 0.18; 'loading': 0.18; 'file,': 0.21; 'header:In-Reply-To:1': 0.22; 'feb': 0.22; 'programs.': 0.23; 'module,': 0.23; 'url:dev': 0.23; 'subject:use': 0.24; 'django': 0.25; 'module': 0.26; 'figure': 0.26; 'all,': 0.27; 'function': 0.27; 'import': 0.27; "who's": 0.28; "i'm": 0.28; 'forgot': 0.29; 'source,': 0.29; 'problem': 0.29; 'pm,': 0.29; 'header,': 0.30; 'logic': 0.30; 'error': 0.30; 'subject:?': 0.31; 'thread': 0.32; 'anyone': 0.32; 'modules': 0.32; 'list': 0.32; 'there': 0.33; "won't": 0.33; 'this.': 0.33; "can't": 0.33; 'header:User-Agent:1': 0.33; 'too': 0.33; 'decide': 0.33; 'file': 0.34; 'header:X-Complaints-To:1': 0.34; 'from:addr:yahoo.co.uk': 0.34; 'done': 0.34; 'all.': 0.34; 'probably': 0.35; 'to:addr:python-list': 0.35; 'form,': 0.35; 'url:python': 0.35; 'beginning': 0.36; 'received:org': 0.36; 'speak': 0.37; 'checks': 0.37; 'run': 0.37; 'reasons': 0.37; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'should': 0.38; 'url:org': 0.39; 'that.': 0.39; 'received:78': 0.40; 'put': 0.40; 'change': 0.40; 'to:addr:python.org': 0.40; 'easy': 0.60; 'your': 0.61; 'you.': 0.62; 'believe': 0.65; 'special': 0.66; 'xxx': 0.71; 'reply,': 0.73; '08:48': 0.84; '09:32': 0.84; 'facts?': 0.84; 'lei': 0.84; 'readability': 0.84; 'received:as13285.net': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: when to use import statements in the header, when to use import statements in the blocks where they are used?
Date Wed, 08 Feb 2012 08:17:59 +0000
References <CACUCHEBhLhCCLL3OOudBKnuD2BZJvDkYzjmoi_nsVR+JPzEENA@mail.gmail.com> <4F31D85B.4040406@davea.name> <CACUCHECoeiU4QfqZJSnd+n9vcORWq=oOFP-aSSS-FayBwhR28g@mail.gmail.com> <4F31E0ED.30908@davea.name>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host host-78-146-7-131.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.0; rv:10.0) Gecko/20120129 Thunderbird/10.0
In-Reply-To <4F31E0ED.30908@davea.name>
X-Antivirus avast! (VPS 120207-1, 07/02/2012), Outbound message
X-Antivirus-Status Clean
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
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.5537.1328689086.27778.python-list@python.org> (permalink)
Lines 72
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1328689086 news.xs4all.nl 6854 [2001:888:2000:d::a6]:58438
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20014

Show key headers only | View raw


On 08/02/2012 02:41, Dave Angel wrote:
> You forgot to include the list in your reply, so I'm forwarding it for
> you. One way you could have done it was to reply-all.
>
>
> On 02/07/2012 09:32 PM, Patto wrote:
>> Dave Angel:
>>
>> On Wed, Feb 8, 2012 at 10:05 AM, Dave Angel<d@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?
>>>> 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/<http://www.python.org/dev/peps/pep-0008/>
>>>
>>>
>>> --
>>>
>>> DaveA
>>>
>>>
>> 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?
>>
>
> I can't speak for django or django-celery. There are people that
> disagree on this, and there are some reasons to override the ones I
> mentioned. One would be large modules that are not used in most
> circumstances, or not used till the program has run for a while.
>
> If you put the import inside a function, you can save on startup time by
> deferring some of the logic till later. And if there's a module that
> probably won't be used at all (eg. an error handler), perhaps you can
> avoid loading it at all.
>
> I still think readability trumps all the other reasons, for nearly all
> programs. Only once you decide you have a performance problem should you
> change that.
>

There's a thread on the dev ml about imports that also discusses startup 
times for anyone who's interested.

-- 
Cheers.

Mark Lawrence.

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? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-02-08 08:17 +0000

csiph-web