Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Computers are useless. They can only give you answers. -- Pablo Picasso


devel / comp.lang.python / Re: Importing modules with arguments

SubjectAuthor
* Importing modules with argumentsCharles Smith
`* Re: Importing modules with argumentsChris Angelico
 `* Re: Importing modules with argumentsCharles Smith
  `- Re: Importing modules with argumentsChris Angelico

1
Importing modules with arguments

<32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:705e:: with SMTP id y30mr3070527qtm.239.1627664105507;
Fri, 30 Jul 2021 09:55:05 -0700 (PDT)
X-Received: by 2002:ac8:7207:: with SMTP id a7mr3167306qtp.32.1627664105219;
Fri, 30 Jul 2021 09:55:05 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Fri, 30 Jul 2021 09:55:05 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=66.131.2.159; posting-account=sVcFPwoAAAAmVofkDk4AGnSdHRcL-J84
NNTP-Posting-Host: 66.131.2.159
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
Subject: Importing modules with arguments
From: char...@sollum.tech (Charles Smith)
Injection-Date: Fri, 30 Jul 2021 16:55:05 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 1999
 by: Charles Smith - Fri, 30 Jul 2021 16:55 UTC

I have found myself wanting to import module and provide arguments to them. There's two main reason I could think of for this. First is to prevent a circular import, though most of circular imports can be prevented by changing the design. The second reason is to invert dependencies between two modules. It occurred to me when using libraries like FastAPI or Flask that it would be nicer to use and leaner if instead of:
1. Import FastAPI Router or FastAPI object directly
2. Create a Router
3. Use the route decorators

We could simply import a module with importlib and provide the router itself. You can then keep all the FastAPI related code inside its module and only have clean list of endpoints. Now this is actually doable right now, here's the snippet: https://gist.github.com/BinarSkugga/c281cbbe36e7f11bc0fd143ea1bb4dd4

Disclaimer: I have no idea what are the implication of this but I'd like to have it as a feature instead of this hack.

Re: Importing modules with arguments

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

  copy mid

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

  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: Importing modules with arguments
Date: Sat, 31 Jul 2021 04:05:24 +1000
Lines: 71
Message-ID: <mailman.299.1627668337.4164.python-list@python.org>
References: <32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
<CAPTjJmo+KrMYi_xcEBHpQgmeB6bttE3bEx0Dwe+K7bJXXiuD7w@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de ZW5D1S1QaLLtkpLJgyb2qQbe973B5deuV8gsaPx0ZsyA==
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=VtFPnelE;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'url-ip:140.82.121/24':
0.03; 'url-ip:140.82/16': 0.03; 'jul': 0.04; '31,': 0.05;
'modules': 0.07; 'package.': 0.07; 'wanting': 0.07; 'confusing,':
0.09; 'dependencies': 0.09; 'module.': 0.09; 'modules.': 0.09;
'namespace': 0.09; 'other.': 0.09; 'url-ip:140/8': 0.12;
'url:github': 0.13; "can't": 0.14; 'import': 0.14; 'problem':
0.15; '"import': 0.16; 'arguments': 0.16; 'chrisa': 0.16;
'doable': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'idiom': 0.16; 'module,': 0.16; 'route': 0.16;
'subject:modules': 0.16; 'wrote:': 0.16; 'says': 0.16; 'syntax':
0.16; 'python': 0.16; 'instead': 0.17; 'libraries': 0.20;
'object': 0.23; 'returns': 0.23; 'sat,': 0.23; "i'd": 0.23;
'to:addr:python-list': 0.23; 'code': 0.24; 'probably': 0.24;
'anything': 0.24; 'idea': 0.25; 'flask': 0.27; 'bit': 0.28;
'module': 0.28; 'this.': 0.29; '(and': 0.30; 'there': 0.31;
'think': 0.31; 'stuff': 0.31; 'but': 0.31; "doesn't": 0.32;
'to:name:python': 0.32; 'message-id:@mail.gmail.com': 0.33;
'using': 0.33; 'class': 0.33; 'received:209.85.166': 0.33; 'header
:In-Reply-To:1': 0.33; 'same': 0.34; 'received:google.com': 0.34;
'reflect': 0.35; 'situation': 0.35; 'from:addr:gmail.com': 0.35;
'able': 0.36; 'inside': 0.37; 'recommend': 0.37; 'two': 0.37;
"that's": 0.37; 'change': 0.37; 'really': 0.37; 'received:209.85':
0.38; 'file': 0.38; 'those': 0.38; 'though': 0.38; "it's": 0.38;
'something': 0.38; 'received:209': 0.38; 'going': 0.38; 'use':
0.38; 'list': 0.39; 'main': 0.40; 'reason': 0.40; 'could': 0.40;
'best': 0.61; 'simply': 0.62; 'here': 0.62; 'gives': 0.63; 'cost':
0.64; 'clear': 0.65; 'back': 0.65; 'parts': 0.66; 'well': 0.66;
'right': 0.66; 'now,': 0.67; 'prevent': 0.68; 'full': 0.68;
'changing': 0.69; 'disclaimer:': 0.82; '2021': 0.84; 'imports':
0.84; 'itself.': 0.84; 'prevented': 0.84; 'stuff,': 0.84; 'of:':
0.91; 'router': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:content-transfer-encoding;
bh=ZwyfkKoTiLcdbYzkA2SRTOux9L4+cZ6eGMPLisY0sjg=;
b=VtFPnelEn/I4CDB9yglWIJNm6JP+EYxVTPP7mfAiHm0Emta3YCOlLimazjECPMN+N6
y74lDbZSa0qXhh5YwilrU7jWZ+8oQb3DK/YQ48HK9ciOhnCXSNWDp0lozAgZyBhYB7Cz
tcDyssQXXXiMsXwI1pGKVSk1TCHmnZ7hVhSwfa0ybHd9G2k4UU42J/4OePF/jLeQLRba
5QotNRLtcJq4FJIwyaI6FAhz1RDQYHH9uMEo1L1yTW1D2tE0vVMLTtaPqNV9Ro8dwF21
ueogUliwr9pVGkF2JiNLMBufMvkkYiG9AGgMSmUX0y/6f1EjB+C3RpkkaIhr1Q0sEGen
8uOg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:content-transfer-encoding;
bh=ZwyfkKoTiLcdbYzkA2SRTOux9L4+cZ6eGMPLisY0sjg=;
b=ilDpR0zfUG2WBXypSOcWtuUVq9uBUlf0IMjWzc3nyHFkPRy6MI5TtjhPDvnJiuXGLY
zmhS911TSwAY+Gfet183TF8rY/Ns0TSgcecOflXyK+GknzUEMxVoz5dpNY4Asd0GYnQ2
4cy+4TPUs0d88oNZXTJcylkZm4RT6xuixT12u2Qoe/lE92mLSsBzAHPfafpBzcKYT5va
QZlf0RhHhkeKh/MrCkWDhzgw1dDsiAVnjBUzbglyqrpKy20aek+irrcvN3cYRgF7ryRv
aKVKwa25/qHs/a8ktfBao7BDFTbpwU3FQG3ZVN5A+TFbhVdncdxJd5Qyl6hIqXSc9wAo
E3eA==
X-Gm-Message-State: AOAM532gCrjIboqcA2LSQJSNSh0uoSSbLBtr6W7gfJqcz17CLmiV4lNk
Bn3+zr+ijUT5OpGYRwS/L3UnGFCNgZUkA1b9KIALPhOI
X-Google-Smtp-Source: ABdhPJw4N3IMC/Vf4pkqMB8t1UQCH97ObC7jsfWa1KRLbZpz3xYfRlxZg0yuZVFhvOBA3Cxzmk9nVvNUsq/Ba8QHD/4=
X-Received: by 2002:a92:cf4d:: with SMTP id c13mr395733ilr.300.1627668335089;
Fri, 30 Jul 2021 11:05:35 -0700 (PDT)
In-Reply-To: <32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <CAPTjJmo+KrMYi_xcEBHpQgmeB6bttE3bEx0Dwe+K7bJXXiuD7w@mail.gmail.com>
X-Mailman-Original-References: <32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
 by: Chris Angelico - Fri, 30 Jul 2021 18:05 UTC

On Sat, Jul 31, 2021 at 3:48 AM Charles Smith <charles@sollum.tech> wrote:
>
> I have found myself wanting to import module and provide arguments to them. There's two main reason I could think of for this. First is to prevent a circular import, though most of circular imports can be prevented by changing the design. The second reason is to invert dependencies between two modules. It occurred to me when using libraries like FastAPI or Flask that it would be nicer to use and leaner if instead of:
> 1. Import FastAPI Router or FastAPI object directly
> 2. Create a Router
> 3. Use the route decorators
>
> We could simply import a module with importlib and provide the router itself. You can then keep all the FastAPI related code inside its module and only have clean list of endpoints. Now this is actually doable right now, here's the snippet: https://gist.github.com/BinarSkugga/c281cbbe36e7f11bc0fd143ea1bb4dd4
>
> Disclaimer: I have no idea what are the implication of this but I'd like to have it as a feature instead of this hack.
>

One problem here is that imports are cached. If module #1 says "import
flask", Python runs the module and gives back the result; if module #2
subsequently says "import flask", Python returns the exact same module
from the cache. That won't work well with parameterization.

I would recommend either:

1) Import a class from the module, then instantiate; or
2) Import the module, then make a change to its globals.

The first one allows different modules to have different parameters;
the second would have different parameters overwrite each other. (And
it would be very clear that it's going to do so.)

The idiom "from flask import Flask; flask = Flask()" is a bit clunky,
but it's probably the best you have for adding parameters. (In this
specific case, "app = Flask()", which is more common, but same
difference.) I can't really imagine shortening it very much; a syntax
like "import flask()" would be confusing, and every other syntax I can
think of will be only marginally better than the standard idiom.

There is one important situation where you can't just import the whole
module and then do stuff, and that's choosing to import *less* than
the full module. For that use-case, it kinda has to be a package, but
then you should be able to do something like:

package/__init__.py
# all the basic stuff in this file

package/expensive1.py
package/expensive2.py
package/expensive3.py
# different parts

package/all.py
from . import expensive1, expensive2, expensive3

Then you can say something like:

import package.expensive2

and it'll only get some part of the package. It's a bit unideal in
that the namespace has to reflect this, but with careful use of
__getattr__, you could conceal those details (at the cost of even more
complexity). On the upside, though, the parameterization doesn't have
to break anything - if module 1 imports one part of the package, and
module 2 imports another part, both parts will coexist nicely.

ChrisA

Re: Importing modules with arguments

<6e08b664-f55a-490a-9625-1b7825f24396n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ad4:4ae6:: with SMTP id cp6mr4304744qvb.23.1627671267821;
Fri, 30 Jul 2021 11:54:27 -0700 (PDT)
X-Received: by 2002:a37:658c:: with SMTP id z134mr3747713qkb.310.1627671267584;
Fri, 30 Jul 2021 11:54:27 -0700 (PDT)
Path: i2pn2.org!i2pn.org!news.uzoreto.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!news-out.netnews.com!news.alt.net!fdc3.netnews.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Fri, 30 Jul 2021 11:54:27 -0700 (PDT)
In-Reply-To: <mailman.299.1627668337.4164.python-list@python.org>
Injection-Info: google-groups.googlegroups.com; posting-host=66.131.2.159; posting-account=sVcFPwoAAAAmVofkDk4AGnSdHRcL-J84
NNTP-Posting-Host: 66.131.2.159
References: <CAPTjJmo+KrMYi_xcEBHpQgmeB6bttE3bEx0Dwe+K7bJXXiuD7w@mail.gmail.com>
<32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com> <mailman.299.1627668337.4164.python-list@python.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6e08b664-f55a-490a-9625-1b7825f24396n@googlegroups.com>
Subject: Re: Importing modules with arguments
From: char...@sollum.tech (Charles Smith)
Injection-Date: Fri, 30 Jul 2021 18:54:27 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2080
 by: Charles Smith - Fri, 30 Jul 2021 18:54 UTC

First off, thanks for the answer. I don't see the cached module as a problem here. If you provide arguments to a module, the goal is "most likely" to alter/parameterize the behavior of the first import. Now, I agree that behavior becomes unpredictable because passing different parameters on subsequent imports won't change the behavior of the imported module . One thing that could be done is to memoize those imports based on the parameters (this is a huge change to how imports works so its a bit yikes). Another solution would be to simply not cache parameterized imports. You sacrifice performance for added flexibility which I think is fair. I do however agree about partial imports. It would be rather complex to properly pass the context but its certainly doable.

Re: Importing modules with arguments

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

  copy mid

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

  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: Importing modules with arguments
Date: Sat, 31 Jul 2021 05:25:16 +1000
Lines: 44
Message-ID: <mailman.300.1627673130.4164.python-list@python.org>
References: <CAPTjJmo+KrMYi_xcEBHpQgmeB6bttE3bEx0Dwe+K7bJXXiuD7w@mail.gmail.com>
<32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
<mailman.299.1627668337.4164.python-list@python.org>
<6e08b664-f55a-490a-9625-1b7825f24396n@googlegroups.com>
<CAPTjJmpTc7Wi3-zGBKnCnE3mYSY+j3THPRNo5HnMD8fDWsMo-Q@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de N2IpSJUwoxLt0kNHqcID5gCVgdNE9jughZ8uOrhAunfA==
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=EUnZbcj7;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.016
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'jul': 0.04; '31,': 0.05;
'partial': 0.07; 'import': 0.14; 'problem': 0.15; '"import': 0.16;
'5:11': 0.16; 'answer.': 0.16; 'arguments': 0.16; 'behaviour':
0.16; 'bypass': 0.16; 'caching': 0.16; 'chrisa': 0.16;
'distance.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris
angelico': 0.16; 'importing': 0.16; 'interesting,': 0.16;
'module,': 0.16; 'statement).': 0.16; 'subject:modules': 0.16;
'wrote:': 0.16; 'language': 0.22; 'behavior': 0.23; 'sat,': 0.23;
'to:addr:python-list': 0.23; 'goal': 0.24; 'code': 0.24; 'bit':
0.28; 'function': 0.28; 'done': 0.28; 'module': 0.28; 'sense':
0.28; 'think': 0.31; 'but': 0.31; '(this': 0.32; 'context': 0.32;
'selection': 0.32; 'to:name:python': 0.32; 'message-
id:@mail.gmail.com': 0.33; 'class': 0.33; 'received:209.85.166':
0.33; 'header:In-Reply-To:1': 0.33; 'received:google.com': 0.34;
'complex': 0.35; 'from:addr:gmail.com': 0.35; 'trying': 0.36;
'inside': 0.37; "that's": 0.37; 'change': 0.37; 'really': 0.37;
'received:209.85': 0.38; 'those': 0.38; "it's": 0.38; 'something':
0.38; 'received:209': 0.38; 'going': 0.38; 'does': 0.38; 'could':
0.40; 'simply': 0.62; 'lot': 0.62; 'pass': 0.64; 'your': 0.64;
'skip:r 20': 0.64; 'becomes': 0.65; 'now,': 0.67; 'offering':
0.67; 'that,': 0.68; 'performance': 0.74; '2021': 0.84;
'attribute': 0.84; 'imports': 0.84; 'time).': 0.84; 'want.': 0.84;
'subsequent': 0.95
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025;
h=mime-version:references:in-reply-to:from:date:message-id:subject:to
:content-transfer-encoding;
bh=IPBwHnFyAYCqxHcyXYjCUD1hSR367D0IPN8BWrovoNU=;
b=EUnZbcj7Be3iJ/088TJK+5g+W+7n70jvmM48oUCmCdDSGlKZUJGiNL9vbB2Q879OEg
Y6NQNDZl6AMuplRMAfkm7L5ZAPizyXHdXrZ/k9sBochcXsHwLi8StLi+Xww80JL3Ueqq
chfwuTEJwALxUkG/IRJ+OpHQr/GYebpDQtK2IJsdFWRKa95/Z3SDkI5UdLGWoh1+RpVr
xyDy4S8P6BiyprPV8Kj8xuuQXtf7lPWrzPl4E5lwH8rGcUad7mm4nHMMdH4pjKx99Keg
J4MLT/57fDl47GK3HLXVUl5a2sjxTEFav+4eEelb5KERCjh6bSZXh1XNkr+qwzbPGp2f
Z21w==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20161025;
h=x-gm-message-state:mime-version:references:in-reply-to:from:date
:message-id:subject:to:content-transfer-encoding;
bh=IPBwHnFyAYCqxHcyXYjCUD1hSR367D0IPN8BWrovoNU=;
b=ZWD/SswjRQ6juGmMhu2Zo/K/agxy4kwOKJJB6PS3kH1GO502pG9Gyb6N9vK4i++E9K
8rjUwaZd5+XOcisO0yP+70YyiFnJSpAxTvLs7+EgNbTp6TYJt3HJCz3klzY7EEzgkIxM
8zNoLtTh3C+F8x45tZPf5rA9rLED8+LNhIOSgOLuRxuodoRVAvLv+59jvob4hf4LI8TY
tLzNP01u7i2I8YocAObOsOIxvKRW02VZNq2Lr4EJ+g53U0L/KbnU+UhrLd/l2KaMzjea
uquPmrc5YPUCvTLWSyzsPedQMf7FbZGkyh0+i2N08JqCgXArHjMXdlsI/mt+AkPQ8UiJ
JhQA==
X-Gm-Message-State: AOAM531imKEbjrqAfP38Rp2DUZswQtWiMj+/bhqKLb6fegkH4kiaf68a
76Kb8rSf+vW3ND9Dy0lICdQd0eieQphfPAz966I2qG0W
X-Google-Smtp-Source: ABdhPJw4WsXhPi07kbUZdDYQmuX5TTlOQSVdm5c80u/vMgidxJG8RI/vukMnS4ChrjiruFddeV5Q5ovQiVhwGh+uR5E=
X-Received: by 2002:a5e:8e02:: with SMTP id a2mr2915870ion.205.1627673127351;
Fri, 30 Jul 2021 12:25:27 -0700 (PDT)
In-Reply-To: <6e08b664-f55a-490a-9625-1b7825f24396n@googlegroups.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.34
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: <CAPTjJmpTc7Wi3-zGBKnCnE3mYSY+j3THPRNo5HnMD8fDWsMo-Q@mail.gmail.com>
X-Mailman-Original-References: <CAPTjJmo+KrMYi_xcEBHpQgmeB6bttE3bEx0Dwe+K7bJXXiuD7w@mail.gmail.com>
<32df0b54-2e76-4c7e-b6cb-23bfd0abf333n@googlegroups.com>
<mailman.299.1627668337.4164.python-list@python.org>
<6e08b664-f55a-490a-9625-1b7825f24396n@googlegroups.com>
 by: Chris Angelico - Fri, 30 Jul 2021 19:25 UTC

On Sat, Jul 31, 2021 at 5:11 AM Charles Smith <charles@sollum.tech> wrote:
>
> First off, thanks for the answer. I don't see the cached module as a problem here. If you provide arguments to a module, the goal is "most likely" to alter/parameterize the behavior of the first import. Now, I agree that behavior becomes unpredictable because passing different parameters on subsequent imports won't change the behavior of the imported module .
>

The trouble is that if any other module has already imported the one
you're trying to parameterize, then your code won't know that the
parameters haven't been applied. It's action at a distance.

> One thing that could be done is to memoize those imports based on the parameters (this is a huge change to how imports works so its a bit yikes).

That's functionally equivalent to importing a class and then
instantiating, but with an automated cache. Could be interesting, but
belongs inside the module, not as a language mechanic, IMO.

> Another solution would be to simply not cache parameterized imports. You sacrifice performance for added flexibility which I think is fair.

And that's functionally equivalent to importing a class and then
instantiating, *without* the automated cache. :)

> I do however agree about partial imports. It would be rather complex to properly pass the context but its certainly doable.
>

Yeah.

So here's a possibility. Craft a module-level __getattr__ function
that does the caching behaviour you want. Then you can "from
modulename import Thing", where the precise name you want is the
selection of parameter. The __getattr__ function can also set it as an
intrinsic attribute (thus offering high performance caching - it'll
bypass __getattr_ the second time). Other than that, though, there's
not really a lot of parameterization that would make sense as part of
the import (if you need to say something like "import random;
random.seed(12345)", that's not really going to fit into the import
statement).

ChrisA

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor