2.1.1 global weirds

Robin Becker robin at jessikat.fsnet.co.uk
Sat Aug 11 06:24:00 EDT 2001


I have difficulty in understanding why the 
 print n, len(getStory()), len(_story)
always prints n, 0, 0

why is b2.getStory not getStory? Is it because importing from a isn't
the same as the importing from the __main__ module?

What is the correct way to import from the main script?

when I run a.py I see
0 0 0
1 0 0
2 0 0
[1, 2]
0


#a.py
_story = []

def getStory():
        return _story

def func(n):
        getStory().append(n)

def run():
        print 0, len(getStory()), len(_story)
        import b1
        print 1, len(getStory()), len(_story)
        import b2
        print 2, len(getStory()), len(_story)
        print b2.getStory()
        print b2.getStory is getStory

if __name__=='__main__':
        run()

#b1.py
from a import getStory, func
func(1)

#b2.py
from a import getStory, func
func(2)
-- 
Robin Becker



More information about the Python-list mailing list