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


Groups > comp.lang.python > #20949

Re: Question about circular imports

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.022
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'imports': 0.07; 'python': 0.08; 'dict': 0.09; 'imported.': 0.09; 'received:mail- lpp01m010-f46.google.com': 0.09; 'am,': 0.12; 'circular': 0.15; '"import': 0.16; 'altogether.': 0.16; 'nail': 0.16; 'uncommon': 0.16; 'cc:addr:python-list': 0.16; 'wrote:': 0.18; 'functions,': 0.18; 'subject:Question': 0.19; 'programming': 0.20; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; 'trying': 0.21; 'header:In- Reply-To:1': 0.22; 'feb': 0.22; 'fine': 0.24; 'cc:2**0': 0.26; 'module': 0.26; 'code': 0.26; 'import': 0.27; 'message- id:@mail.gmail.com': 0.29; 'importing': 0.29; 'rid': 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'seem': 0.29; 'sun,': 0.30; 'received:209.85.215.46': 0.32; 'does': 0.32; 'yet': 0.32; 'modules': 0.32; "won't": 0.33; 'normally': 0.34; 'all.': 0.34; 'faq': 0.34; 'something': 0.35; 'question': 0.36; 'two': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'received:209.85': 0.38; '2nd': 0.38; 'some': 0.38; 'think': 0.38; 'fail': 0.39; 'possible,': 0.39; 'clearly': 0.39; 'received:209.85.215': 0.39; 'received:209': 0.39; 'called': 0.40; 'header:Received:6': 0.61; 'address': 0.61; 'frank': 0.64; 'busy': 0.65; 'grab': 0.66; '1st': 0.70; '26,': 0.73; 'battle': 0.84; 'outset': 0.84; 'received:10.152': 0.84; 'locally': 0.91; 'recurring': 0.93
Received-SPF pass (google.com: domain of ian.g.kelly@gmail.com designates 10.152.130.102 as permitted sender) client-ip=10.152.130.102;
Authentication-Results mr.google.com; spf=pass (google.com: domain of ian.g.kelly@gmail.com designates 10.152.130.102 as permitted sender) smtp.mail=ian.g.kelly@gmail.com; dkim=pass header.i=ian.g.kelly@gmail.com
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=ELog0OAW+1OqSIITWeRkTAHzH+2rnJWSp9eUOjVIFmQ=; b=kFFdhEyNCTG2+QqopKTyU+94y3PlLgdtGsBhGXGwhV4P/fQ/IghCO+OCUL+7XeS1aS TvRViYhPU2SSkJxbEMEaro+/0owylZUWFwgX7ZoNoOmOpT+SM1tpIzMOZVlJFCWLBRMS 5bVi2xsTBVRmxbx0CmlScVYr8oQlyUfzkIwO8=
MIME-Version 1.0
In-Reply-To <jid2a9$n21$1@dough.gmane.org>
References <jid2a9$n21$1@dough.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Mon, 27 Feb 2012 11:36:12 -0700
Subject Re: Question about circular imports
To Frank Millman <frank@chagford.com>
Content-Type text/plain; charset=ISO-8859-1
Cc python-list@python.org
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.207.1330367803.3037.python-list@python.org> (permalink)
Lines 35
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1330367803 news.xs4all.nl 6907 [2001:888:2000:d::a6]:50550
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:20949

Show key headers only | View raw


On Sun, Feb 26, 2012 at 3:42 AM, Frank Millman <frank@chagford.com> wrote:
> Hi all
>
> I seem to have a recurring battle with circular imports, and I am trying to
> nail it once and for all.
>
> Let me say at the outset that I don't think I can get rid of circular
> imports altogether. It is not uncommon for me to find that a method in
> Module A needs to access something in Module B, and a method in Module B
> needs to access something in Module A. I know that the standard advice is to
> reorganise the code to avoid this, and I try to do this where possible, but
> for now I would like to address the question of how to handle the situation
> if this is otherwise unavoidable.
>
> The problem is clearly explained in the Python Programming FAQ -
>
> "Circular imports are fine where both modules use the "import <module>" form
> of import. They fail when the 2nd module wants to grab a name out of the
> first ("from module import name") and the import is at the top level. That's
> because names in the 1st are not yet available, because the first module is
> busy importing the 2nd."
>
> [SNIP]
>
> I can think of two solutions - one is cumbersome, the other may not be good
> practice.

Solution 3: don't do the circular imports at the top level.  It's
perfectly fine to do the imports locally inside the functions that
need them, which resolves the circularity since normally the functions
won't be called until the module is fully imported.  It does add some
overhead to the functions, basically the cost of a dict lookup.

Cheers,
Ian

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


Thread

Re: Question about circular imports Ian Kelly <ian.g.kelly@gmail.com> - 2012-02-27 11:36 -0700

csiph-web