Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

You mean you didn't *know* she was off making lots of little phone companies?


devel / comp.lang.python / Re: Imports and dot-notation

SubjectAuthor
o Re: Imports and dot-notationCameron Simpson

1
Re: Imports and dot-notation

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Imports and dot-notation
Date: Thu, 10 Aug 2023 09:22:41 +1000
Lines: 92
Message-ID: <mailman.229.1691629413.23016.python-list@python.org>
References: <5f4f8962-08e6-ed0a-8c44-e83a2b10e0ba@schinagl.nl>
<ZNQfwSrBDVkiTi16@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii; format=flowed
X-Trace: news.uni-berlin.de YsNYN2EC+RdSByu44zGSqwvP44UAb4Pq0lYXP4PyXw7w==
Cancel-Lock: sha1:ILfBfhAy9AtO524kY5qkNR12skk= sha256:Jvt5vZhzhFIOdBpeeVar0soxzi3mtcz1e3xVFfNX9YQ=
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(which': 0.04;
'repository': 0.05; 'maintainers': 0.07; 'modules': 0.07;
'programmer': 0.07; 'spaces': 0.07; 'thing.': 0.07; 'cc:addr
:python-list': 0.09; 'code?': 0.09; 'depend': 0.09; 'example.':
0.09; 'imports,': 0.09; 'likewise': 0.09; 'obviously': 0.09;
'skip:` 10': 0.09; 'cheers,': 0.11; 'coding': 0.13; 'cc:no real
name:2**0': 0.14; 'import': 0.15; '(due': 0.16; '(eg': 0.16;
'cameron': 0.16; 'complete,': 0.16; 'from:addr:cs': 0.16;
'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16;
'message-id:@cskk.homeip.net': 0.16; 'modules,': 0.16;
'readability': 0.16; 'simpson': 0.16; 'skip:> 10': 0.16; 'skip:>
20': 0.16; 'stdlib': 0.16; 'structure.': 0.16; 'styles,': 0.16;
'typing': 0.16; 'use:': 0.16; 'usual,': 0.16; 'verbose': 0.16;
'wrote:': 0.16; 'python': 0.16; 'name.': 0.19;
'cc:addr:python.org': 0.20; 'maybe': 0.22; 'code': 0.23;
'cc:2**0': 0.25; 'notes': 0.26; 'so.': 0.26; 'bit': 0.27; 'etc':
0.28; 'example,': 0.28; 'header:User-Agent:1': 0.30; 'code,':
0.31; 'seem': 0.31; 'module': 0.31; 'saved': 0.31; 'negative':
0.32; 'personally': 0.32; 'but': 0.32; "i'm": 0.33; 'particular':
0.33; 'header:In-Reply-To:1': 0.34; 'particularly': 0.35; 'also,':
0.36; 'functions': 0.36; 'people': 0.36; 'source': 0.36; 'really':
0.37; "it's": 0.37; 'directory': 0.37; 'though': 0.37; 'best,':
0.38; 'two': 0.39; 'single': 0.39; 'enough': 0.39; 'use': 0.39;
'on.': 0.39; 'advantage': 0.40; 'both': 0.40; 'something': 0.40;
'method': 0.61; 'here.': 0.61; 'above': 0.62; 'basis': 0.62;
'reasonable': 0.62; 'skip:b 10': 0.63; 'experience': 0.64;
'personal': 0.64; 'clear': 0.64; 'full': 0.64; 'becomes': 0.64;
'your': 0.64; 'similar': 0.65; 'received:userid': 0.66; 'time.':
0.66; 'header:Received:6': 0.67; 'end,': 0.69; 'piece': 0.69;
'choice': 0.76; 'short,': 0.76; 'beauty.': 0.84; 'cleaner': 0.84;
'imports': 0.84; 'lean': 0.84; 'oliver': 0.84; 'received:119':
0.84; 'say,': 0.84; 'styles': 0.84; 'visually': 0.84; 'fan': 0.91
Mail-Followup-To: Oliver Schinagl <oliver+python@schinagl.nl>,
python-list@python.org
Content-Disposition: inline
In-Reply-To: <5f4f8962-08e6-ed0a-8c44-e83a2b10e0ba@schinagl.nl>
User-Agent: Mutt/2.2.7 (2022-08-07)
X-tce-id: cskk@bigpond.com
X-tce-ares-id: e{702a0765-d43f-4f47-8e0c-f0a866917807}1
X-tce-spam-action: no action
X-tce-spam-score: 0.0
X-Cm-Analysis: v=2.4 cv=JJ798eGb c=1 sm=1 tr=0 ts=64d43270
a=86NgPeer/26qwRe8d7OrCw==:117 a=86NgPeer/26qwRe8d7OrCw==:17
a=kj9zAlcOel0A:10 a=UttIx32zK-AA:10 a=CvQ3IjafAAAA:8 a=peoOfDobDoudd7NazAYA:9
a=CjuIK1q_8ugA:10 a=BG2cHyFXwb1O1LB579zk:22
X-Cm-Envelope: MS4xfL9abpgOjNEhw35CuXiiC1pwor2FzxDh8IGDOR7OomFL7bo7dlzLfLwYemhGCEStBj06GathTxrqouQH2wiT/TEKRcEEJ51Yw0fCVyM7YnqxL0D68JTV
wnXg3/uDFDTqV+FTl2r5e7TSWb/bfYWc8elJNmx0NRmqxzboM0nMi+I6SvRV+hk0Wo08PLdaWeHxsQ==
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: <ZNQfwSrBDVkiTi16@cskk.homeip.net>
X-Mailman-Original-References: <5f4f8962-08e6-ed0a-8c44-e83a2b10e0ba@schinagl.nl>
 by: Cameron Simpson - Wed, 9 Aug 2023 23:22 UTC

On 09Aug2023 12:30, Oliver Schinagl <oliver+python@schinagl.nl> wrote:
>Looking at a python projects code and repository layout, we see the
>following directory structure.
>
>
>/project/core
>/project/components/module1
>...
>/project/components/moduleN
>/projects/util
>
>(this is far from complete, but enough to help paint a picture.
>
>Some modules import other modules, and I see (at the very least) two
>(kind of three?) different ways of doing so.
>
>`from project.components.module1 import function1, function2 as func,
>CONST1, CONST2 as CONST`
>
>or maybe even (which has as an advantage that it becomes clear which
>namespace something belongs to
>
>`from project.components.module1 import function1, function2 as
>module1_function2, CONST1, CONST2 as MODULE1_CONST2`
>
>but then it really just becomes personal preference, as the number of
>characters saved on typing is almost negative (due to having a more
>complex import).
>
>
>but also the dot notation being used
>
>`from project.components import module1`
>
>where obviously the functions are invoked as `module1.function1` etc
>
>I hope the above is clear enough from an example.
>
>
>Simply put, which of the two would be considered cleaner and more pythonic?

This is almost entirely a programmer preference thing.

The Zen, as usual, offers guideance but not dictates.

As you say, the module1.func form is very clear and has its beauty.
Personally I lean towards the single name flavour, sometimes with a
rename. FOr example, with os.path I often go:

from os.path import join as joinpath

and likewise for several other names from there, because the bare names
are very generic (eg "join"). I see to be alone here. Many other people
use:

import os.path

and use the full os.path.join in their code, which I find jarring and
visually noisy.

>Now for a bit more thought, looking at PEP8, we notes about imports,
>but sadly PEP8 does not state which method is better/preferred. While
>obviously in the end, it's always the maintainers choice in what they
>prefer, so are tabs vs spaces and PEP8 is opinionated about that too
>(in a good way, even though I'm not a fan :p).

PEP8 is for the stdlib source code; that said it is also the basis for
most Python coding styles in the wild.

For me, the Zen's "readability counts" is the most important guideline;
and to this end, your "module1.function" is an entirely reasonable
response, particularly if you've got a lot of imports. But it does make
for more verbose code, and that works against readability to a degree.

This is why it's all subjective.

Particular workplaces may mandate particular styles, but in your
personal code? Do what seem best, and experience will cause that to
evolve over time.

Also, the style can depend on the code you're working on. For a small
module with few imports the:

from module1 import funcname

can be a win because the code is short, and this short form of funcname
is both clear and readable. But in a very long piece of code with many
imports you might go for module1.funcname for clarity, particularly if
funcname is generic or overlaps with another similar imported name.

Cheers,
Cameron Simpson <cs@cskk.id.au>


devel / comp.lang.python / Re: Imports and dot-notation

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor