from package import * without overwriting similarly named functions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reckoner

    from package import * without overwriting similarly named functions?


    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.
  • Mike Driscoll

    #2
    Re: from package import * without overwriting similarly namedfunctions?

    On Oct 24, 1:06 pm, Reckoner <recko...@gmail .comwrote:
    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 reallylongnamed package 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

    Comment

    • Tim Chase

      #3
      Re: from package import * without overwriting similarly namedfunctions?

      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.subit em", 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.Scroll bar", prefixing with "Tkinter." for everything.

      -tkc




      Comment

      • Rex

        #4
        Re: from package import * without overwriting similarly namedfunctions?

        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

        Comment

        • Lawrence D'Oliveiro

          #5
          Re: from package import * without overwriting similarly named functions?

          In message
          <80503fdf-7792-4571-825c-27e0c5a59cb5@u2 9g2000pro.googl egroups.com>,
          Reckoner wrote:
          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.

          Comment

          • Lie Ryan

            #6
            Re: from package import * without overwriting similarly namedfunctions?

            On Fri, 24 Oct 2008 11:06:54 -0700, Reckoner wrote:
            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 *


            Comment

            Working...