why import, reload, import again?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beliavsky@aol.com

    #1

    why import, reload, import again?

    Near the beginning of file test_matrix.py from scipy are the lines

    import scipy.base
    reload(scipy.ba se)
    from scipy.base import *
    del sys.path[0]

    Could someone please explain why the first two lines were included? A
    similar script I wrote works fine without them. Also, what is the
    purpose of the "del" line? (I understand the mechanics of what "del"
    does.) The scipy developers are skilled Python programmers, so I am
    trying to understand the idioms used in their codes.

  • Peter Hansen

    #2
    Re: why import, reload, import again?

    beliavsky@aol.c om wrote:[color=blue]
    > Near the beginning of file test_matrix.py from scipy are the lines
    >
    > import scipy.base
    > reload(scipy.ba se)
    > from scipy.base import *
    > del sys.path[0]
    >
    > Could someone please explain why the first two lines were included? A
    > similar script I wrote works fine without them.[/color]

    It would very likely be solely for testing purposes, and only because
    test_matrix.py could be run (after importing it, as opposed to via
    os.system) from another script which also runs other tests.

    Doing the above has the effect of ensuring that scipy.base is reloaded
    prior to the "from scipy.base import *" line. Without the first two,
    the test_matrix.py script could be getting various globals from
    scipy.base which were modified during a previous test. (This smells a
    bit... tests shouldn't be polluting scipy.base, and scipy.base probably
    shouldn't be doing anything magic that requires that reload in the first
    place, but there may be good reasons for it.)

    (I recognize that "idiom" from having had to do it at the interactive
    prompt with a module where I was doing "from xxx import *".)
    [color=blue]
    > Also, what is the
    > purpose of the "del" line? (I understand the mechanics of what "del"
    > does.) The scipy developers are skilled Python programmers, so I am
    > trying to understand the idioms used in their codes.[/color]

    I can only guess it's something similar... actually, I won't guess here
    as the only guesses I can think of would invalidate my theory above. :-)

    -Peter

    Comment

    • Robert Kern

      #3
      Re: why import, reload, import again?

      beliavsky@aol.c om wrote:[color=blue]
      > Near the beginning of file test_matrix.py from scipy are the lines
      >
      > import scipy.base
      > reload(scipy.ba se)
      > from scipy.base import *
      > del sys.path[0]
      >
      > Could someone please explain why the first two lines were included? A
      > similar script I wrote works fine without them. Also, what is the
      > purpose of the "del" line? (I understand the mechanics of what "del"
      > does.) The scipy developers are skilled Python programmers, so I am
      > trying to understand the idioms used in their codes.[/color]

      Pearu probably knows. You should ask on the scipy list as this is almost
      certainly a holdover from the older version of scipy.

      --
      Robert Kern
      robert.kern@gma il.com

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      Working...