Re: [Python-Dev] Imports with underscores

2017-01-10 Thread Chris Barker
On Mon, Jan 9, 2017 at 12:29 PM, Terry Reedy  wrote:

> The tkinter doc still has
>
> ...to use Tkinter all you need is a simple import statement:
>   import tkinter
> Or, more often:
>   from tkinter import *
>
> Should this be changed?
>

yes, it should.

I would suggest suggesting something like:

import tkinter as tk

following the similar convention of wxPython, numpy, matplotlib, (all
large widely used packages that started their lives with import *
recommendations...)

-CHB




-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Terry Reedy

On 1/9/2017 11:48 AM, Guido van Rossum wrote:

I would focus on changing habits to discourage "import *" rather than


The tkinter doc still has

...to use Tkinter all you need is a simple import statement:
  import tkinter
Or, more often:
  from tkinter import *

Should this be changed?


uglifying all new code with this "os as _os" pattern. Very occasionally
one designs a module to explicitly support "import *", and that usually
entails using __all__ (like it or not), making the problem go away
without uglifying the code.


tkinter does not have have __all__.  It would have 160 (in 3.6) minus at 
least 3 (for enum, re, and sys) entries.


--
Terry Jan Reedy

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Daniel Holth
Easily solved with the totally evil ninja mode pattern of module
initialization. It has yet to catch on.

def ninja():
global exported
import os
def exported():
# do something
ninja()
del ninja

On Mon, Jan 9, 2017 at 1:13 PM Sven R. Kunze  wrote:

> Interesting to see that others have the same problem.
>
> We also had this kind of "over-protective" behavior. As far as I know, our
> devs stopped doing it as it feels cumbersome.
>
>
> Another argument for this is: when using PyCharm, this IDE will suggest
> imports from those modules which aren't the original ones. So, you might
> import from a third-party module. Over time, however, people learn to pick
> the "right" module to import from.
>
> Cheers,
> Sven
>
>
> On 09.01.2017 12:42, Steve Holden wrote:
>
> One of my developers recently submitted a pull request incuding a number
> of lines like
>
> import os as _os
>
> When I asked him why he suggested a) this would improve encapsulation, and
> b) the practice was supported in the stdlib. Further investigation reveals
> that some modules (e.g. argparse, crypt, difflib, random) do use this
> technique, but it is far from universal.
>
> So I thought it would be useful to get input from current devs about the
> value of this practice, since to me it seems somewhat anti-pythonic. What
> advantages does it confer?
>
> regards
> Steve Holden
>
>
> ___
> Python-Dev mailing 
> listPython-Dev@python.orghttps://mail.python.org/mailman/listinfo/python-dev
>
>
> Unsubscribe: 
> https://mail.python.org/mailman/options/python-dev/srkunze%40mail.de
>
>
> ___
> Python-Dev mailing list
> Python-Dev@python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe:
> https://mail.python.org/mailman/options/python-dev/dholth%40gmail.com
>
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Sven R. Kunze

Interesting to see that others have the same problem.

We also had this kind of "over-protective" behavior. As far as I know, 
our devs stopped doing it as it feels cumbersome.



Another argument for this is: when using PyCharm, this IDE will suggest 
imports from those modules which aren't the original ones. So, you might 
import from a third-party module. Over time, however, people learn to 
pick the "right" module to import from.


Cheers,
Sven


On 09.01.2017 12:42, Steve Holden wrote:
One of my developers recently submitted a pull request incuding a 
number of lines like


import os as _os

When I asked him why he suggested a) this would improve encapsulation, 
and b) the practice was supported in the stdlib. Further investigation 
reveals that some modules (e.g. argparse, crypt, difflib, random) do 
use this technique, but it is far from universal.


So I thought it would be useful to get input from current devs about 
the value of this practice, since to me it seems somewhat 
anti-pythonic. What advantages does it confer?


regards
Steve Holden


___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/srkunze%40mail.de


___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Barry Warsaw
On Jan 09, 2017, at 06:23 PM, André Malo wrote:

>- __all__ again: it's tedious and error-prone to maintain.

Only if you use the wrong tools 

http://public.readthedocs.io/en/latest/
http://bugs.python.org/issue26632

Cheers,
-Barry
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Terry Reedy

On 1/9/2017 6:42 AM, Steve Holden wrote:

One of my developers recently submitted a pull request incuding a number
of lines like

import os as _os

When I asked him why he suggested a) this would improve encapsulation,
and b) the practice was supported in the stdlib. Further investigation
reveals that some modules (e.g. argparse, crypt, difflib, random) do use
this technique, but it is far from universal.



So I thought it would be useful to get input from current devs about the
value of this practice, since to me it seems somewhat anti-pythonic.
What advantages does it confer?


If the module does not define __all__, it prevents * imports of the 
module from also importing the imported modules.  For instance:


>>> sys
Traceback (most recent call last):
  File "", line 1, in 
sys
NameError: name 'sys' is not defined
>>> from tkinter import *
>>> sys

>>> enum

>>> itertools
Traceback (most recent call last):
  File "", line 1, in 
itertools
NameError: name 'itertools' is not defined

Use of such undocumented and unintended attributes of a module is fragile.

1.  The imported module could be changed.  Tkinter's 'import enum' in 
only used in "class EventType(str, enum.Enum):".  The import could well 
be changed to 'from enum import Enum' or even better, 'from enum import 
Enum as _Enum' and the one use modified.


2. The importing module could be changed.  'from tkinter import *' might 
be changed to 'from tkinter import tk, ...' or 'import tkinter as tk' or 
even replaced by another module.


--
Terry Jan Reedy

___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Guido van Rossum
I would focus on changing habits to discourage "import *" rather than
uglifying all new code with this "os as _os" pattern. Very occasionally one
designs a module to explicitly support "import *", and that usually entails
using __all__ (like it or not), making the problem go away without
uglifying the code.

-- 
--Guido van Rossum (python.org/~guido)
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Ethan Furman

On 01/09/2017 03:42 AM, Steve Holden wrote:


When I asked him why he suggested
 a) this would improve encapsulation, and
 b) the practice was supported in the stdlib.
 Further investigation reveals that some modules (e.g. argparse, crypt,
 difflib, random) do use this technique, but it is far from universal.

So I thought it would be useful to get input from current devs about
 the value of this practice, since to me it seems somewhat anti-pythonic.
 What advantages does it confer?


Aside from what Barry said it also offers the (very) minor advantage of showing 
up as an implementation detail when someone does a dir() of the module.

Personally, I tend to use the technique for new code, but I wouldn't change old 
code for it (and new code in an old module should follow the old module's 
practices, etc.) .

--
~Ethan~
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Paul Moore
On 9 January 2017 at 11:42, Steve Holden  wrote:
> One of my developers recently submitted a pull request incuding a number of
> lines like
>
> import os as _os
>
> When I asked him why he suggested a) this would improve encapsulation, and
> b) the practice was supported in the stdlib. Further investigation reveals
> that some modules (e.g. argparse, crypt, difflib, random) do use this
> technique, but it is far from universal.
>
> So I thought it would be useful to get input from current devs about the
> value of this practice, since to me it seems somewhat anti-pythonic. What
> advantages does it confer?

As I understand it, it prevents the imports showing up in "import *"
imports, but using __all__ is better. I'd imagine usage in the stdlib
is mainly historical.

It's not a practice I'd recommend in user code. The needs of the
stdlib are somewhat special (and stdlib code can be many years old,
reflecting out of date design rules) and "because the stdlib does it"
is not necessarily a compelling argument in the absence of an actual
justification.

Regarding "improves encapsulation", as Barry mentioned, using __all__
(and avoiding import-* anyway) is a better approach. Furthermore,
Python does not encourage strict (as in, enforced, encapsulation).
This is very much a "consenting adults" situation - by hiding imports
like this, you make it harder to monkeypatch (for testing purposes,
for example). Users are meant to stick to the published API of a
module *because it's the right thing to do*, not because they are
forced to.

Paul
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


[Python-Dev] Imports with underscores

2017-01-09 Thread Steve Holden
One of my developers recently submitted a pull request incuding a number of
lines like

import os as _os

When I asked him why he suggested a) this would improve encapsulation, and
b) the practice was supported in the stdlib. Further investigation reveals
that some modules (e.g. argparse, crypt, difflib, random) do use this
technique, but it is far from universal.

So I thought it would be useful to get input from current devs about the
value of this practice, since to me it seems somewhat anti-pythonic. What
advantages does it confer?

regards
Steve Holden
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com


Re: [Python-Dev] Imports with underscores

2017-01-09 Thread Barry Warsaw
On Jan 09, 2017, at 11:42 AM, Steve Holden wrote:

>So I thought it would be useful to get input from current devs about the
>value of this practice, since to me it seems somewhat anti-pythonic. What
>advantages does it confer?

It just means you can't accidentally import it with a from-import-* since
those ignore underscored names by default.

(But then if you use __all__'s it won't happen anyway because you'll never put
'os' in __all__.)

(Aside from the fact that from-import-* is usually bad form, and the problem
with __all__  [1].)

Cheers,
-Barry

[1] http://public.readthedocs.io/en/latest/#the-problem
___
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com