from package import * without overwriting similarly named functions?

R

Reckoner

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?

Thanks.
 
M

Mike Driscoll

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?

Thanks.

You can't do something like this:

from package1 import bork
from package2 import bork

and expect python to know that you want the bork from the first
package at one point and the other at another point. The latter will
basically overwrite the former. You should just import them like this:

import package1
import package2

package1.bork()
package2.bork()

Then Python will know what to do. If the name of the package is long,
you can do this too:

import reallylongnamedpackage as p

then it would be p.bork()

Then again, python is open source. Thus, you can modify the source to
do whatever you want if you have the patience and the knowledge to do
so.

Mike
 
T

Tim Chase

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
 
R

Rex

If you're concerned about specific individual functions, you can use:

from package1 import some_function as f1
form package2 import some_function as f2
 
L

Lawrence D'Oliveiro

In message
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?

Avoid wildcard imports.
 
L

Lie Ryan

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?

That (overwritten names) is exactly the reason why wildcard import should
be avoided.

Use:
from package1 import blah
import package2

But avoid:
from package3 import *
 
F

Fernando H. Sanches

Also, remember that since the latter functions will always overwrite
the first, you can just reverse the order of the imports:

from package2 import *
from package1 import *

This should preserve the functions of package1 over the other ones.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top