from package import * without overwriting similarly named functions?

Tim Chase python.list at tim.thechases.com
Fri Oct 24 14:27:27 EDT 2008


> I have multiple packages that have many of the same function names. Is
> it possible to do
> 
> from package1 import *
> from package2 import *
> 
> without overwriting similarly named objects from package1 with
> material in package2? How about a way to do this that at least gives a
> warning?

Yeah, just use

   from package2 import *
   from package1 import *

then nothing in package1 will get tromped upon.

However, best practices suggest leaving them in a namespace and 
not using the "import *" mechanism for precisely this reason. 
You can always use module aliasing:

   import package1 as p1
   import package2 as p2

so you don't have to type "package1.subitem", but can instead 
just write "p1.subitem".  I prefer to do this with packages like 
Tkinter:

   import Tkinter as tk

   ...
   tk.Scrollbar...

so it doesn't litter my namespace, but also doesn't require me to 
type "Tkinter.Scrollbar", prefixing with "Tkinter." for everything.

-tkc







More information about the Python-list mailing list