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


Groups > comp.lang.python > #63741

Re: Is it better to import python modules inside function or at the top? What are the pros and cons?

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'importing': 0.05; 'found,': 0.07; 'function,': 0.09; 'loaded,': 0.09; 'subject:modules': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'jan': 0.12; 'cached': 0.16; 'cons': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'module).': 0.16; 'script?': 0.16; 'subject: \n ': 0.16; 'subject:import': 0.16; 'subject:top': 0.16; 'subject:python': 0.16; 'wrote:': 0.18; 'module': 0.19; 'normally': 0.19; 'import': 0.22; 'cc:addr:python.org': 0.22; 'load': 0.23; 'module,': 0.24; 'cc:2**0': 0.24; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'especially': 0.30; 'message- id:@mail.gmail.com': 0.30; 'subject:the': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'done': 0.36; 'subject:?': 0.36; 'two': 0.37; 'performance': 0.37; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; '12,': 0.39; 'called': 0.40; 'subject:? ': 0.60; 'most': 0.60; 'first': 0.61; 'soon': 0.63; 'sam': 0.68; 'clearer': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type:content-transfer-encoding; bh=Tgh2HcYD5uSfgk7lmP/tOC2M7hskBriBeG/dnBJMiUo=; b=FtpTBm4RnK1CQ9/+iqmqCQ6Cw5KzZcV4Tr077uXs/iAYYqpqb5KCLpv6WeSw/auGaT cmcAKOCxlPeYOMUpzSOOJQrmW7I+NbzNztuUGDsuo4EE0/rsbZGLIk+KMlPZxZFw1391 Dsvv36rosBZNid9lU+k6F/da8sD6clPJ2bEk5vWVT6LQY1s4F2jyyTQaqOLVQY9iFeiW bQKtJFBiyX1utCH2HqvYMYna9nnZz9J/gY+sVcXkFIQZEkDy11fUnxY5NEL5PPTo1e/R WSJUH2RRtY/EIVzm+6lF1npziRZ8OUMurz+hvZ7P6hZI116eMR543f04IDX2bqIO2YTr Dj8g==
MIME-Version 1.0
X-Received by 10.66.2.2 with SMTP id 2mr21345840paq.87.1389491290541; Sat, 11 Jan 2014 17:48:10 -0800 (PST)
In-Reply-To <ccb82f25-c007-4e90-97d9-5108463ed51f@googlegroups.com>
References <ccb82f25-c007-4e90-97d9-5108463ed51f@googlegroups.com>
Date Sun, 12 Jan 2014 12:48:10 +1100
Subject Re: Is it better to import python modules inside function or at the top? What are the pros and cons?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.5349.1389491293.18130.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1389491293 news.xs4all.nl 2965 [2001:888:2000:d::a6]:48108
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63741

Show key headers only | View raw


On Sun, Jan 12, 2014 at 12:28 PM, Sam <lightaiyee@gmail.com> wrote:
> I have python modules which are used only in specific functions and the functions are not called all the time. Is it better to import the function inside the function only or is it a better practice to always import all modules at the top of the script? If I import the module inside the function, will it cause a big performance hit because of the import module action that gets to be called every time the function is called?
>
> What are the pros and cons of each approach?

Most of the work of importing is still going to be done only once
(after that, the import just grabs a cached reference to the same
module). It's normally clearer to import at the top of the module,
especially if you have multiple functions calling on the same module,
but there are two other concerns:

1) If the module can't be found, would you rather know as soon as your
module is loaded, or is it better to load your module without it and
then fail only when that function is called?

2) If the module takes a long time to load, would you rather pay that
cost as soon as your module is loaded, or on the first use of the
function that needs it?

ChrisA

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


Thread

Is it better to import python modules inside function or at the top? What are the pros and cons? Sam <lightaiyee@gmail.com> - 2014-01-11 17:28 -0800
  Re: Is it better to import python modules inside function or at the top? What are the pros and cons? Chris Angelico <rosuav@gmail.com> - 2014-01-12 12:48 +1100
  Re: Is it better to import python modules inside function or at the top? What are the pros and cons? Ned Batchelder <ned@nedbatchelder.com> - 2014-01-11 20:49 -0500

csiph-web