AW: The rules of reference

Gerson Kurz Gerson.Kurz at t-online.de
Thu Oct 31 14:39:36 EST 2002


Jeff Epler wrote:

> 60 was assigned to __main__.num, but a.num was only set once (to 42)

OK, I see that now. But, being of the adventurous sort (even though I
probably can't spell that word) as I am, I now try this:
--------------------
num = 42

def print_num():
    global num
    print "a.print_num() called, num is %d" % num

if __name__ == "__main__":
    import b
    # remove everything from __main__, use stuff from a instead.
    del print_num
    del num
    try:
        print num
    except:
        print "ok, num does not exist any more."
    from a import *
    print_num()
    # this proves that num can be referenced
    print "in __main__ num is now %d" % num
    num = 60
    print "in __main__ num is now %d" % num
    print_num()
    b.do_stuff()
--------------------
My reasoning: I remove any reference to __main__a, and test it. Then I
reimport a. That should work, right?

ok, num does not exist any more.
a.print_num() called, num is 42
in __main__ num is now 42
in __main__ num is now 60
a.print_num() called, num is 42
b.do_stuff() called
a.print_num() called, num is 42

hmmmmmmm.







More information about the Python-list mailing list