Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #20004
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'disagree': 0.07; 'function,': 0.07; 'imports': 0.07; 'override': 0.07; 'subject:when': 0.07; 'url:peps': 0.09; 'am,': 0.12; 'thanks!': 0.14; 'startup': 0.15; '(eg.': 0.16; '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; 'wed,': 0.17; 'wrote:': 0.18; '>>>': 0.18; 'functions,': 0.18; 'loading': 0.18; 'to:2**1': 0.21; '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; "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; '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; '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; 'speak': 0.37; 'checks': 0.37; 'run': 0.37; 'reasons': 0.37; 'received:192': 0.38; 'could': 0.38; 'some': 0.38; 'think': 0.38; 'should': 0.38; 'url:org': 0.39; 'that.': 0.39; '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; 'header:Reply-To:1': 0.70; 'xxx': 0.71; 'reply-to:no real name:2**0': 0.72; 'reply,': 0.73; '08:48': 0.84; '09:32': 0.84; 'facts?': 0.84; 'lei': 0.84; 'readability': 0.84 |
| Date | Tue, 07 Feb 2012 21:41:49 -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 | Patto <pat.inside@gmail.com>, PYTHON <python-list@python.org> |
| 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> <4F31D85B.4040406@davea.name> <CACUCHECoeiU4QfqZJSnd+n9vcORWq=oOFP-aSSS-FayBwhR28g@mail.gmail.com> |
| In-Reply-To | <CACUCHECoeiU4QfqZJSnd+n9vcORWq=oOFP-aSSS-FayBwhR28g@mail.gmail.com> |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 8bit |
| X-Provags-ID | V02:K0:7NeIL2Ch45+7NX2xV4IfsDTDXHl2dJx/y+AKTrCcqTw BPosfoGCXeDxJyPP8CY+A5rJpqIHqg7bQg+6Vu2mfQGfG1mbDs A42QM6zXr4tiEx8xhnQ+IkzrjjGCw9h7K5uFH5ayq2u8ql0RuK WTvhPoHtGoKRHqhXe16Cd0fLMpQqzpDCIjHlsZRGVZpAsGdf7J N27mA9Is5crOAcVgj8YviAdVWTmVwSHQOB3olTX5VdLU8rVmNA U/RVm7BVdulbLXPhvep2iCksJUOr94phTCj/jgG8PzdShyL2tt PcqufmZcRKK66hl8ynjKP+XrO2BB2o65BtAxMbg+8sFYeSXQCm Zn4Cy1m8QA7A8gA90RkA= |
| 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.5531.1328668916.27778.python-list@python.org> (permalink) |
| Lines | 63 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1328668916 news.xs4all.nl 6851 [2001:888:2000:d::a6]:44129 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:20004 |
Show key headers only | View raw
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. -- DaveA
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll 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:41 -0500 Re: when to use import statements in the header, when to use import statements in the blocks where they are used? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-02-09 03:32 +0000
csiph-web