Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"A dirty mind is a joy forever." -- Randy Kunkee


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

SubjectAuthor
* Imports and dot-notationOliver Schinagl
`- Re: Imports and dot-notationStefan Ram

1
Imports and dot-notation

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

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: oliver+p...@schinagl.nl (Oliver Schinagl)
Newsgroups: comp.lang.python
Subject: Imports and dot-notation
Date: Wed, 9 Aug 2023 12:30:51 +0200
Lines: 119
Message-ID: <mailman.226.1691596731.23016.python-list@python.org>
References: <5f4f8962-08e6-ed0a-8c44-e83a2b10e0ba@schinagl.nl>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de hzoVtFOKqmMqSwQluXaiTgP/hKP5vLqfsCl0UJKLABSA==
Cancel-Lock: sha1:F839fZj7foB6dr2I1jd3yM/0Rh4= sha256:OWwYRjycq3MOE6U60x5IvkjiFzJI2Ue6jFjPDc8ovAM=
Return-Path: <oliver+python@schinagl.nl>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="1024-bit key; unprotected key"
header.d=schinagl.nl header.i=@schinagl.nl header.b=T4pAYoZl;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.018
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; '(which': 0.04;
'repository': 0.05; 'hopefully': 0.07; 'maintainers': 0.07;
'modules': 0.07; 'pep': 0.07; 'spaces': 0.07; '-\xc2\xa0': 0.09;
'better,': 0.09; 'enough.': 0.09; 'example.': 0.09; 'imports,':
0.09; 'namespace': 0.09; 'obviously': 0.09; 'readable': 0.09;
'skip:` 10': 0.09; 'import': 0.15; '(due': 0.16; '(without': 0.16;
'better?': 0.16; 'complete,': 0.16; 'explicit': 0.16; 'likewise,':
0.16; 'modules,': 0.16; 'namespaces': 0.16; 'novice': 0.16;
'one?': 0.16; 'personally,': 0.16; 'preferably': 0.16;
'readability': 0.16; 'refuse': 0.16; 'structure.': 0.16; 'typing':
0.16; 'python': 0.16; "aren't": 0.19; 'to:addr:python-list': 0.20;
'maybe': 0.22; 'code': 0.23; 'list,': 0.24; "i'd": 0.24; 'idea':
0.24; 'examples': 0.25; 'seems': 0.26; 'notes': 0.26; 'so.': 0.26;
'bit': 0.27; 'coming': 0.27; 'community,': 0.28; 'etc': 0.28;
'suggest': 0.28; 'it,': 0.29; 'header:User-Agent:1': 0.30;
'whole': 0.30; 'code,': 0.31; 'saved': 0.31; 'think': 0.32;
"doesn't": 0.32; 'question': 0.32; '(this': 0.32; 'from,': 0.32;
'negative': 0.32; 'topic,': 0.32; 'but': 0.32; "i'm": 0.33;
'there': 0.33; 'same': 0.34; 'complex': 0.35; 'following': 0.35;
'functions': 0.36; 'people': 0.36; 'those': 0.36; "skip:' 10":
0.37; 'really': 0.37; 'using': 0.37; "it's": 0.37; 'directory':
0.37; 'though': 0.37; 'way': 0.38; 'two': 0.39; 'quite': 0.39;
'enough': 0.39; 'mentioned': 0.39; 'use': 0.39; 'advantage': 0.40;
'ide': 0.40; 'both': 0.40; 'something': 0.40; 'should': 0.40;
'method': 0.61; 'remember': 0.61; 'skip:h 10': 0.61; "there's":
0.61; 'above': 0.62; 'dear': 0.62; 'point.': 0.62; 'simply': 0.63;
'feel': 0.63; 'great': 0.63; 'everything': 0.63; 'skip:b 10':
0.63; 'personal': 0.64; 'clear': 0.64; 'becomes': 0.64; 'came':
0.65; 'look': 0.65; 'less': 0.65; 'let': 0.66; 'lead': 0.67;
'worked': 0.67; 'generally': 0.67; 'end,': 0.69; 'skip:/ 10':
0.69; 'strong': 0.69; 'within': 0.69; 'terms': 0.70;
'received:185': 0.71; 'little': 0.73; 'deal': 0.73; '....': 0.76;
'choice': 0.76; 'favor': 0.76; 'potentially': 0.76; 'base,': 0.84;
'characters': 0.84; 'cleaner': 0.84; 'flat': 0.84; 'from.': 0.84;
'incorrect': 0.84; 'readability.': 0.84; 'reading,': 0.84; 'fan':
0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=schinagl.nl; s=7of9;
t=1691577051; bh=eIDaFI4lynCrhmUpbrJd+tfvYB5J4nu7+uo/tp2HYrU=;
h=Date:From:Subject:To;
b=T4pAYoZl+iTvrDXB0c3V7913AeeP7FuOew58wfsNaJt+05xQdaeHrT/SCV+qxPbt9
RbWjnWAOJsD1c7g5c+MPnTIoM9Pm4G7lEsSdlO3HL+iXr9odSct0TgilBtFfypuf5F
bQHrdv0sTauJ5axwEg6pcPuMR7SSegwwv+hTM9nI=
User-Agent: Mozilla Thunderbird
Content-Language: nl
X-Mailman-Approved-At: Wed, 09 Aug 2023 11:58:50 -0400
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: <5f4f8962-08e6-ed0a-8c44-e83a2b10e0ba@schinagl.nl>
 by: Oliver Schinagl - Wed, 9 Aug 2023 10:30 UTC

Dear list,

First a disclaimer, I am a python novice ;) so apologies beforehand for
any incorrect terms and use thereof :)

I have a question about the preferred/pythonic way dealing with imports.
But let me start by giving a little bit of an example (which lead me to
this question).

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?

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).

Likewise, if we look at PEP20, there's quite a few 'guidelines' that
suggest the dot-notation. To name a few (opinionated) examples (but note
I am just using PEP20 as a helper to give some arguments, I know some
people feel strongly against pep20 :p):

* Beautiful is better than ugly.
  - Personally, I think the dot notation is more beautiful, but I
understand that some people find the short notation more beautiful and
less of an eye sore ;)

* Explicit is better than implicit.
  - To me, this is a strong point. Being more explicit is always
better, using the dot notation (if though it's not doing the WHOLE path)
is a nice balance in being explicit enough. Code readability comes from
being more explicit (without being to terse of course).

* Simple is better than complex.
* Flat is better than nested.
  -  I think having more flat and simple imports, and (while more
characters to potentially type in the invocation) more readable
invocations would be better?

* Readability counts.
  - The dot notation seems to help in readability. For those unfamiliar
(or less familiar) with a code base, it becomes incredibly clear where a
something comes from (WITHOUT having to rely on IDE's; sure you can
hover over something and see that the IDE has hopefully worked out where
something came from, but this doesn't work when just reading a page of
code, you aren't going to hover over everything and remember all that
context).

* In the face of ambiguity, refuse the temptation to guess.
  - I'd argue this is almost self-explanatory, not using dot-notation
makes it more ambiguous where something is coming from.

* There should be one-- and preferably only one --obvious way to do it.
  - This is where my confusion/question obviously comes from, as
there's multiple ways of doing it, so maybe PEP8 (or a new one) should
more strongly favor one?

* Namespaces are one honking great idea -- let's do more of those!
  - Basically, this question/proposal would 'force/nudge' you into
writing more with namespaces by using the namespace as prefix.

So what IS the preferred/more pythonic/recommended way to deal with imports?

Why was this not mentioned in PEP8?

Is it worth while to introduce a new PEP or amend PEP8?

For what it's worth, hallucinating chatgpt/google bard both say the same
about this topic, that within the python community, it is generally
preferred and a good idea to use dot-notation. But how correct are they lol.

Thank you for reading,

Olliver

Re: Imports and dot-notation

<imports-20230809172809@ram.dialup.fu-berlin.de>

  copy mid

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

  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: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Imports and dot-notation
Date: 9 Aug 2023 16:42:21 GMT
Organization: Stefan Ram
Lines: 44
Expires: 1 Sep 2024 11:59:58 GMT
Message-ID: <imports-20230809172809@ram.dialup.fu-berlin.de>
References: <mailman.226.1691596731.23016.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de S7xcmoBDZhirQQLbBxXePQYxQhWuEUo5N0PaEC1eVYxFuo
Cancel-Lock: sha1:x5zhQYE7CJEmbekCENKrCxrB8PY= sha256:Ufu2s0udaFXJ3a2oBQH4ZCF+kLRpnqdfEAh9tr4Q16M=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Wed, 9 Aug 2023 16:42 UTC

Oliver Schinagl <oliver+python@schinagl.nl> writes:
>Simply put, which of the two would be considered cleaner and
>more pythonic?

I'm not sure what "clean" or "pythonic" means here.

To me, the canonical import is

import math
print( math.sin( 0 ))

, because "math.sin" shows where "sin" does come from.
This is valid for production code.

For tiny code examples it might look nicer to use

from math import sin
print( sin( 0 ))

, but I would try to avoid

from math import sin as sine
print( sine( 0 ))

because it hides the common name of the object.

When there is a nested module,

import tkinter.messagebox
tkinter.messagebox.showinfo( "ok.", "It's alright." )

, as this won't work:

import tkinter
tkinter.messagebox.showinfo( "ok.", "It's alright." )

. But for small code examples:

from tkinter.messagebox import showinfo
showinfo( "ok.", "It's alright." )

.

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor