New submission from Terry J. Reedy:

Tkinter naming was designed so that 'from tkinter import *' can work, in the 
sense of not conflicting with built-in and stdlib module names.  But there are 
currently two problems.

1. The current doc

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

over-promotes 'import *' as the common and therefore normal import (as opposed 
to a convenience for interactive use).  It neglects the alternatives 'import 
tkinter as tk' or 'from tkinter import Tk, ...', and consequently makes no 
mention of the relative advantages.

2. The current code does not define __all__.  So the stdlib imports of enum, 
re, and sys get carried over with 'import *'.  Guido recommends defining 
__all__ to prevent this.*  Since tkinter defines about 160 names, and since 
there are only 3 names to block, and there are used pretty sparingly, I prefer 
the uglier alternative of renaming with underscores: 'import enum as _enum', 
etc.

I will work on patches.  Since the doc change can apply to all current versions 
while the code change might be restricted to 3.7, I will keep then separate.


* From pydev thread 'Imports with underscores', 2017-1-9:

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

----------
components: Tkinter
messages: 286996
nosy: serhiy.storchaka, terry.reedy
priority: normal
severity: normal
stage: test needed
status: open
title: Improve tkinter 'import *' situation
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue29446>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to