Can someone explain this behavior to me?

J

Jesse Aldridge

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()
 
C

Chris Rebert

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"

Not sure, but circular imports are *evil* anyway, so I'd suggest you
just rewrite the code to avoid doing any circular imports in the first
place.

Cheers,
Chris
 
J

John Machin

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"

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.
 
A

Albert Hopkins

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"

AFAICT you have 2 different "foo" modules here. The first foo is when
foo.py is called as a script, but it's not called "foo" it's called
"__main__" because it's called as a script. When "bar" is imported, it
imports "foo", but this is different. Technically this is the first time
you are *importing* foo. It's actually loaded a second time with the
name "foo".

A more simplified version of it is this:

$ cat foo.py
cat = 6
import bar
print '%s: %s.cat = %s' % (__file__, __name__, cat)

$ cat bar.py
import foo
foo.cat = 7
print '%s: %s.cat = %s' % (__file__, foo.__name__, foo.cat)

$ python foo.py
/home/marduk/test/foo.py: foo.cat = 6
/home/marduk/test/bar.py: foo.cat = 7
foo.py: __main__.cat = 6


OTOH:
$ python -c "import foo"
bar.py: foo.cat = 7
foo.py: foo.cat = 7

But, as others have said, this is confusing and should be avoided.

-a
 
S

Steve Holden

Jesse said:
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"

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top