Can someone explain this behavior to me?

Steve Holden steve at holdenweb.com
Thu Feb 26 19:37:49 EST 2009


Jesse Aldridge wrote:
> I have one module called foo.py
> ---------------------
> class Foo:
>     foo = None
> 
> def get_foo():
>     return Foo.foo
> 
> if __name__ == "__main__":
>     import bar
>     Foo.foo = "foo"
>     bar.go()
> ---------------------
> And another one called bar.py
> ---------------------
> import foo
> 
> def go():
>     assert foo.get_foo() == "foo"
> ----------------------
> When I run foo.py, the assertion in bar.py fails.  Why?

There are actually two get_foo()s, since foo.py is run (giving it the
name "__main__") and imported (giving it the name "foo"). You will
probably find that __main__.get_foo() == "foo".

regards
 Steve

-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list