Can someone explain this behavior to me?

John Machin sjmachin at lexicon.net
Thu Feb 26 17:44:40 EST 2009


On Feb 27, 8:48 am, Jesse Aldridge <JesseAldri... at gmail.com> 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?

AFAICT from that convoluted mess, because there are two rabbit holes,
__main__.Foo.foo and foo.Foo.foo, and you poked "foo" down the wrong
one. In any case the other one is not the right one -- as you have
already been advised, circular imports are evil.



More information about the Python-list mailing list