Reload() Confusion

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

    Reload() Confusion

    I'm going to be teaching EEs some basic Python using the first few
    chapters of Learning Python, 2nd ed. by Mark Lutz. The discussion on
    Reloading Modules starting on page 266 is confusing and I believe
    incorrect. On page 266 it says that a reload "changes the existing
    module object in place." That's a little vague, but on page 267 it
    says "every reference to a module object anywhere in your program is
    automatically affected by a reload."

    It is my understanding that the reloaded objects become *new* objects
    in memory, available only via a fully-qualified reference to the new
    module. The old objects remain in memory until the last reference to
    them is gone.

    I've re-written the section on Reload Basics, and I would like to get
    some comments, both on correctness and clarity.

    The UA College of Engineering with 18 engineering degrees is the destination of choice for students serious about making a difference in the world.


    Your help will be appreciated, both by me and by the students
    struggling through this the first time.

    -- Dave

  • Mel Wilson

    #2
    Re: Reload() Confusion

    In article <u4l450hv7isekp k09upvhcpi3b18m 3p8a7@4ax.com>,
    David MacQuigg <dmq@gain.com > wrote:[color=blue]
    >I'm going to be teaching EEs some basic Python using the first few
    >chapters of Learning Python, 2nd ed. by Mark Lutz. The discussion on
    >Reloading Modules starting on page 266 is confusing and I believe
    >incorrect. On page 266 it says that a reload "changes the existing
    >module object in place." That's a little vague, but on page 267 it
    >says "every reference to a module object anywhere in your program is
    >automaticall y affected by a reload."
    >
    >It is my understanding that the reloaded objects become *new* objects
    >in memory, available only via a fully-qualified reference to the new
    >module. The old objects remain in memory until the last reference to
    >them is gone.[/color]

    It seems to me that the module keeps its identity over
    the reload:

    Python 2.3 (#46, Jul 29 2003, 18:54:32) [MSC v.1200 32 bit (Intel)] on win32
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> import re
    >>> id(re)[/color][/color][/color]
    9940176[color=blue][color=green][color=darkred]
    >>> reload(re)[/color][/color][/color]
    <module 're' from 'E:\bin\Python2 3\lib\re.py'>[color=blue][color=green][color=darkred]
    >>> id(re)[/color][/color][/color]
    9940176[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]
    [color=blue]
    >
    >I've re-written the section on Reload Basics, and I would like to get
    >some comments, both on correctness and clarity.
    >
    >http://ece.arizona.edu/~edatools/Python/Reload.htm
    >
    >Your help will be appreciated, both by me and by the students
    >struggling through this the first time.
    >[/color]
    I agree with you. It seems that the module code is
    re-executed in the previously-existing modules namespace.
    Wherever that code created new objects the first time, it
    will create brand-new objects the second time. Small
    integers and interned strings will keep their identities (of
    course), as will objects referenced through other modules.
    Nothing is explicitely deleted, so bindings can be left over
    from the modules prior incarnation.
    No other namespaces will be affected except when code in the
    module explicitely calls for it.

    For what it's worth, the following code:

    #============== =========
    """M1 version 1"""
    a=1
    b=2
    c=[1]
    d=[2]

    #============== =========
    """M1 version 2"""
    a=1
    b=4
    c=[3]
    e=[4]

    #============== =========
    """test_reload. py"""
    import os, shutil
    def module_contents (M):
    print "Doc: ", M.__doc__
    for k, v in M.__dict__.item s():
    if not k.startswith ("__"):
    print "%s:\t%d\t% s" % (k, id(v), v)

    def grab_contents (M):
    d = {}
    for k, v in M.__dict__.item s():
    if not k.startswith ("__"):
    d[k] = v
    return d

    #os.remove ("M1.pyc")
    shutil.copyfile ("M1_1.py", "M1.py")
    import M1
    module_contents (M1)
    mc = grab_contents (M1)

    print
    os.remove ("M1.pyc")
    shutil.copyfile ("M1_2.py", "M1.py")
    reload (M1)
    module_contents (M1)

    print '\nMC:'
    for k, v in mc.items():
    print "%s:\t%d\t% s" % (k, id(v), v)




    Which gives

    F:\home\mwilson \Projects\pytho n\testscript>py thon test_reload.py
    Doc: M1 version 1
    a: 8463376 1
    c: 9940432 [1]
    b: 8470496 2
    d: 9940368 [2]

    Doc: M1 version 2
    a: 8463376 1
    c: 9940272 [3]
    b: 8468480 4
    e: 9937584 [4]
    d: 9940368 [2]

    MC:
    a: 8463376 1
    c: 9940432 [1]
    b: 8470496 2
    d: 9940368 [2]


    The second os.remove seems to be required in case the reloaded
    version of M1.py is older than the imported one.

    Regards. Mel.

    Comment

    • Skip Montanaro

      #3
      Re: Reload() Confusion

      [color=blue][color=green]
      >> It is my understanding that the reloaded objects become *new* objects
      >> in memory, available only via a fully-qualified reference to the new
      >> module. The old objects remain in memory until the last reference to
      >> them is gone.[/color][/color]

      Mel> It seems to me that the module keeps its identity over the reload:

      Yup. The module remains the same, but its __dict__ is repopulated.

      Skip

      Comment

      • David MacQuigg

        #4
        Re: Reload() Confusion

        I like this code example because it gets the student to think about
        how modules are kept in memory. Showing the module's dictionary
        before and after a reload is great. I'll try to integrate it into my
        presentation, perhaps in place of the second example, which can now
        become an exercise. Thanks.

        -- Dave

        On Sat, 20 Mar 2004 17:55:17 -0500, mwilson@the-wire.com (Mel Wilson)
        wrote:
        [color=blue]
        >For what it's worth, the following code:
        >
        >#============= ==========
        >"""M1 version 1"""
        >a=1
        >b=2
        >c=[1]
        >d=[2]
        >
        >#============= ==========
        >"""M1 version 2"""
        >a=1
        >b=4
        >c=[3]
        >e=[4]
        >
        >#============= ==========
        >"""test_reload .py"""
        >import os, shutil
        >def module_contents (M):
        > print "Doc: ", M.__doc__
        > for k, v in M.__dict__.item s():
        > if not k.startswith ("__"):
        > print "%s:\t%d\t% s" % (k, id(v), v)
        >
        >def grab_contents (M):
        > d = {}
        > for k, v in M.__dict__.item s():
        > if not k.startswith ("__"):
        > d[k] = v
        > return d
        >
        >#os.remove ("M1.pyc")
        >shutil.copyfil e ("M1_1.py", "M1.py")
        >import M1
        >module_content s (M1)
        >mc = grab_contents (M1)
        >
        >print
        >os.remove ("M1.pyc")
        >shutil.copyfil e ("M1_2.py", "M1.py")
        >reload (M1)
        >module_content s (M1)
        >
        >print '\nMC:'
        >for k, v in mc.items():
        > print "%s:\t%d\t% s" % (k, id(v), v)
        >
        >
        >
        >
        >Which gives
        >
        >F:\home\mwilso n\Projects\pyth on\testscript>p ython test_reload.py
        >Doc: M1 version 1
        >a: 8463376 1
        >c: 9940432 [1]
        >b: 8470496 2
        >d: 9940368 [2]
        >
        >Doc: M1 version 2
        >a: 8463376 1
        >c: 9940272 [3]
        >b: 8468480 4
        >e: 9937584 [4]
        >d: 9940368 [2]
        >
        >MC:
        >a: 8463376 1
        >c: 9940432 [1]
        >b: 8470496 2
        >d: 9940368 [2]
        >
        >
        >The second os.remove seems to be required in case the reloaded
        >version of M1.py is older than the imported one.
        >
        > Regards. Mel.[/color]

        Comment

        • Mel Wilson

          #5
          Re: Reload() Confusion

          In article <g2qs50hvt1p0n2 1icb0jka43cct9m fah0q@4ax.com>,
          David MacQuigg <dmq@gain.com > wrote:[color=blue]
          >I like this code example because it gets the student to think about
          >how modules are kept in memory. Showing the module's dictionary
          >before and after a reload is great. I'll try to integrate it into my
          >presentation , perhaps in place of the second example, which can now
          >become an exercise. Thanks.[/color]

          Use it in good health.

          Mel.

          Comment

          Working...