[Tutor] Star imports, was Re: How to get a Tkinter window to print a color copy of itself as a .pdf file?

Peter Otten __peter__ at web.de
Thu Apr 16 09:54:03 CEST 2015


boB Stepp wrote:

>> import Tkinter as tk
> 
> Question: I have been using "from Tkinter import *" as suggested in
> "Programming Python" by Lutz. He remarks that unlike other situations,
> this is generally safe with Tkinter. Is there a benefit to doing the
> import as you have?

It's a stylistic preference: I like to keep namespaces separate.

When I make an exception, typically for generic functionality like

contextmanager, groupby, namedtuple, defaultdict...

I import these in a controlled way with

from collections import defaultdict
from itertools import islice, zip_longest

etc., use them as "pseudo-builtins", and avoid writing functions with the 
same name myself. By contrast I have no clear idea of what's in the tkinter 
package and might write my own getint() function, say, thus forcing a reader 
to scan the complete module to learn which getint() is used.

This is a general problem of star imports; when you overwrite an imported 
name you typically don't care or even know about that name in the imported 
module. When you read your module later to fix or amend something your 
knowledge may have grown, and you expect the imported function where the 
custom function is used. This confusion becomes even more likely when a 
second developer is involved.



More information about the Tutor mailing list