Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

I just thought of something funny...your mother. -- Cheech Marin


devel / comp.lang.python / Re: Circular Import

SubjectAuthor
* Circular ImportStefano Ovus
`* Re: Circular ImportStefano Ovus
 `- Re: Circular ImportChris Angelico

1
Circular Import

<e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17792&group=comp.lang.python#17792

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a0c:f8ca:0:b0:444:41e8:89b1 with SMTP id h10-20020a0cf8ca000000b0044441e889b1mr9588800qvo.22.1649777291051;
Tue, 12 Apr 2022 08:28:11 -0700 (PDT)
X-Received: by 2002:a37:9487:0:b0:67e:81c3:506b with SMTP id
w129-20020a379487000000b0067e81c3506bmr3567535qkd.254.1649777290882; Tue, 12
Apr 2022 08:28:10 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 12 Apr 2022 08:28:10 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=77.89.32.150; posting-account=GCcIsAoAAACTxFaku4imFdrfbyTis2CQ
NNTP-Posting-Host: 77.89.32.150
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>
Subject: Circular Import
From: stefano....@gmail.com (Stefano Ovus)
Injection-Date: Tue, 12 Apr 2022 15:28:11 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 24
 by: Stefano Ovus - Tue, 12 Apr 2022 15:28 UTC

How can I avoid circular imports keeping separated modules ?

-- square.py

from circle import Cirle
class Square:
def __init__(self):
...
@classmethod
def from_circle(cls, circle: Circle) -> Square:
...
return cls(...)

-- circle.py

from square import Square
class Circle:
def __init__(self):
...
@classmethod
def from_square(cls, square: Square) -> Circle:
...
return cls(...)

Re: Circular Import

<0b89e077-d198-4c1d-a1f8-779cc3f898ddn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17794&group=comp.lang.python#17794

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a37:c13:0:b0:69c:231e:5b48 with SMTP id 19-20020a370c13000000b0069c231e5b48mr4004935qkm.278.1649784178489;
Tue, 12 Apr 2022 10:22:58 -0700 (PDT)
X-Received: by 2002:a05:620a:371a:b0:69c:3954:14be with SMTP id
de26-20020a05620a371a00b0069c395414bemr3897073qkb.648.1649784178302; Tue, 12
Apr 2022 10:22:58 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 12 Apr 2022 10:22:58 -0700 (PDT)
In-Reply-To: <e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=77.89.32.150; posting-account=GCcIsAoAAACTxFaku4imFdrfbyTis2CQ
NNTP-Posting-Host: 77.89.32.150
References: <e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <0b89e077-d198-4c1d-a1f8-779cc3f898ddn@googlegroups.com>
Subject: Re: Circular Import
From: stefano....@gmail.com (Stefano Ovus)
Injection-Date: Tue, 12 Apr 2022 17:22:58 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 10
 by: Stefano Ovus - Tue, 12 Apr 2022 17:22 UTC

found a solution: importing modules instead of classes, ex.

-- square.py

import circle

....
@classmethod
def from_circle(cls, circle: circle.Circle) -> Square:
...
return cls(...)

Re: Circular Import

<mailman.78.1649785674.20749.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=17795&group=comp.lang.python#17795

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ros...@gmail.com (Chris Angelico)
Newsgroups: comp.lang.python
Subject: Re: Circular Import
Date: Wed, 13 Apr 2022 03:47:41 +1000
Lines: 25
Message-ID: <mailman.78.1649785674.20749.python-list@python.org>
References: <e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>
<0b89e077-d198-4c1d-a1f8-779cc3f898ddn@googlegroups.com>
<CAPTjJmoPhB+cdBagUA5c=dQpwEAjS3RgaOnWhwdUt1CxjzuQPQ@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de k6T1VxY1TVCCwL+fI3yI9wVBxavtlEob9OfXMWOJqVNw==
Return-Path: <rosuav@gmail.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=gmail.com header.i=@gmail.com header.b=JuAzHoaj;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'circle': 0.07; 'modules': 0.07; 'methods,': 0.09; 'import': 0.15;
'@classmethod': 0.16; 'chrisa': 0.16; 'classes,': 0.16;
'executed': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'importing': 0.16; 'solution:': 0.16; 'wrote:':
0.16; 'instead': 0.17; 'to:addr:python-list': 0.20; 'problem,':
0.22; 'code': 0.23; 'classes': 0.26; 'solution.': 0.26; 'module':
0.31; '(as': 0.32; 'message-id:@mail.gmail.com': 0.32; 'but':
0.32; 'work.': 0.34; 'header:In-Reply-To:1': 0.34;
'received:google.com': 0.34; 'runs': 0.35; 'from:addr:gmail.com':
0.35; 'those': 0.36; 'received:209.85': 0.37; 'received:209':
0.39; 'methods': 0.39; 'wed,': 0.39; 'both': 0.40; 'simply': 0.63;
'subject:Circular': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to;
bh=DCxmdWwvOld0+vjYRFWuvjQz6j78IWDfwn/vEDn2J5E=;
b=JuAzHoajyl8fxTQkG5KXKa4q0ReAL86Qr6rJ2YhZWBu/WBArVxuBlVKWO9KzclqODz
r5cB4NDOTalkFqNrrUgWXKjDZP7J21laC0Aa6W9a0qi6cGBz+AGtVzVlFC/vSOybQUcE
oGCB16wYUEOivera24Om3qT4HNkHSWjdjehb6Wp6Ihx/LqWSuA7yL4JlCwyb0cRbQv4l
CiVN3LMIEHCONY/iLDWB6IqcrZ+JIlkZnUWEXVOZ8Ti9uqHjI9gzDGNiVnDSrsXHebvh
EWia+aZCxE2vIo00SlODST0EaTqmrJv2C8+/uIhiYimKUU4LRLxrrqR3wS44VkoFxa7M
JRhg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20210112;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to;
bh=DCxmdWwvOld0+vjYRFWuvjQz6j78IWDfwn/vEDn2J5E=;
b=a1siMnH29f3CbAaUhWnNV0MdFL7XdvC23WG9/IQBjWaF9DrXYofFd3bj3CGxpF9TQD
5hYmEGSMWftRc84mTDZIUhcAv5tWGbpHitGZGkLyRpGA/RSVcALRADqWesgpVdjrtmfm
JRwan0cL/W8IGfarMaFTHCA6JPYDWXqf443CcVn/PAYKhlJQ0MQ7SKx5+hQ4GcaQ9XPi
OhrBpKBIm8Si6iYWFjcbsLPoddgqvTOElnBOuA/EpVvCsNxThRUr5ES0mXognt/anCRo
IcDf1D9zaPhGkJgav04uncVksL20Qcx0bwx1c1arBFDz3Z5cAgSgtTKzhK2W21bZiDyu
hkCg==
X-Gm-Message-State: AOAM532qBnEwVzS7qLfdc20K4hgAqfK8+3O+l+Fsr/ZlGu6C3A+/NAQU
2pSoEqs0saV2CJCUB3AXWjspoxQn+E+ZC+NclcVmiTjE
X-Google-Smtp-Source: ABdhPJw+rqTnERgPq7IZLA/nxpWJ/3vYDJOkLh9mIqiC1Aol5NSSPqSsnCgvvoGXc5MQFniUJ8JQTC9GUN/sI00AX54=
X-Received: by 2002:a5d:64e7:0:b0:204:b62:94c3 with SMTP id
g7-20020a5d64e7000000b002040b6294c3mr29383096wri.243.1649785672299; Tue, 12
Apr 2022 10:47:52 -0700 (PDT)
In-Reply-To: <0b89e077-d198-4c1d-a1f8-779cc3f898ddn@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
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: <https://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>
X-Mailman-Original-Message-ID: <CAPTjJmoPhB+cdBagUA5c=dQpwEAjS3RgaOnWhwdUt1CxjzuQPQ@mail.gmail.com>
X-Mailman-Original-References: <e0175bb3-9c0b-44c3-a7f0-f47eaf6556fan@googlegroups.com>
<0b89e077-d198-4c1d-a1f8-779cc3f898ddn@googlegroups.com>
 by: Chris Angelico - Tue, 12 Apr 2022 17:47 UTC

On Wed, 13 Apr 2022 at 03:37, Stefano Ovus <stefano.ovus@gmail.com> wrote:
>
> found a solution: importing modules instead of classes, ex.
>
> -- square.py
>
> import circle
>
> ...
> @classmethod
> def from_circle(cls, circle: circle.Circle) -> Square:
> ...
> return cls(...)

Yep! Good solution.

Be aware that this will only work if both modules have fully executed
before the references are actually needed. Usually not a problem, but
if you have code that runs at top-level that needs to refer to the
other module (as opposed to simply defining classes and methods, where
those methods are going to refer to the other module when they get
called), it might not work. But that's no different from any other
circular dependency.

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor