Deutsch   English   Français   Italiano  
<uu68ip$8tb8$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Mark Bourne <nntp.mbourne@spamgourmet.com>
Newsgroups: comp.lang.python
Subject: Re: A missing iterator on itertools module?
Date: Fri, 29 Mar 2024 11:27:20 +0000
Organization: A noiseless patient Spider
Lines: 54
Message-ID: <uu68ip$8tb8$1@dont-email.me>
References: <66059eb6$0$7522$426a34cc@news.free.fr>
 <solution-20240328180624@ram.dialup.fu-berlin.de>
 <6605a515$0$2578$426a74cc@news.free.fr>
 <underscore-20240328182256@ram.dialup.fu-berlin.de>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 29 Mar 2024 11:27:22 +0100 (CET)
Injection-Info: dont-email.me; posting-host="202926c5daed1b08bb375afddd391b79";
	logging-data="292200"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX181t6nQkxD9LIYeMl/hKMai"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
 SeaMonkey/2.53.18.1
Cancel-Lock: sha1:fAwhkPlwC/6l5+dqOL9YTKZqMmw=
In-Reply-To: <underscore-20240328182256@ram.dialup.fu-berlin.de>
Bytes: 3240

Stefan Ram wrote:
> ast <none@none.fr> wrote or quoted:
>> Why did you renamed itertools as _itertools ?
> 
>    Assume I have a module A.py:
> 
> import math
> def f(): pass
> 
>    . Assume I have an additional module B.py:
> 
> import A
> 
>    . Now, when I'm editing "B.py" in IDLE and type "A.", IIRC
>    IDLE will offer me two possible completions: "A.math" and
>    "A.f". The "A.math" makes no sense to me.

`import math` imports the `math` module and binds it to `math` in the 
global namespace of the `A` module.  Since it doesn't have a leading 
underscore, by default it's considered to be a public attribute of the 
`A` module, and IDLE is offering all the public attributes of the `A` 
module for completion.

> I want it to go
>    away. Therefore, I rewrite A.py as:
> 
> import math as _math
> def f(): pass
> 
>    . Now, Idle will only offer the completion "A.f".
> 
>    So, I sometimes use this "import math as _math" style. But then,
>    it is simpler for me to /always/ use this style; after all: you
>    can't know whether someone eventually will import your module!

You can explicitly declare the public interface of a module by defining 
`__all__`, listing the names which should be considered part of the 
module's public interface; see:
- https://docs.python.org/3/reference/simple_stmts.html#the-import-statement
- https://peps.python.org/pep-0008/#public-and-internal-interfaces

Although `from A import *` is generally discouraged, if `A` defines 
`__all__` then only the names listed in `__all__` are bound in the 
importing module's namespace.  Otherwise, all names from `A` which don't 
have a leading underscore are considered to be public and bound in the 
importing module.

I don't use IDLE, but it may be that it also uses `__all__` to determine 
a module's public API.  In that case, setting `__all__ = ["f"]` in `A` 
should prevent it from offering `math` as a completion (nor any other 
name that's not in the `__all__` list).

-- 
Mark.