[Assorted-commits] SF.net SVN: assorted:[1041] sandbox/trunk/src/py/module-globals
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-10-27 20:20:34
|
Revision: 1041 http://assorted.svn.sourceforge.net/assorted/?rev=1041&view=rev Author: yangzhang Date: 2008-10-27 20:20:21 +0000 (Mon, 27 Oct 2008) Log Message: ----------- extended the global modules demo Modified Paths: -------------- sandbox/trunk/src/py/module-globals/globs.py sandbox/trunk/src/py/module-globals/main.py Modified: sandbox/trunk/src/py/module-globals/globs.py =================================================================== --- sandbox/trunk/src/py/module-globals/globs.py 2008-10-27 07:19:30 UTC (rev 1040) +++ sandbox/trunk/src/py/module-globals/globs.py 2008-10-27 20:20:21 UTC (rev 1041) @@ -1,3 +1,5 @@ def foo(): global x x = 'hello' + +v = raw_input() Modified: sandbox/trunk/src/py/module-globals/main.py =================================================================== --- sandbox/trunk/src/py/module-globals/main.py 2008-10-27 07:19:30 UTC (rev 1040) +++ sandbox/trunk/src/py/module-globals/main.py 2008-10-27 20:20:21 UTC (rev 1041) @@ -3,7 +3,14 @@ # Demonstrates that an imported module cannot affect the globals seen by the # outer (importing) modules. +# Also demonstrates that you can change things like sys.stdin out from under +# another module before importing, so that even their in-line code is affected. +from cStringIO import StringIO +import sys +sys.stdin = StringIO('abc') from globs import * +import globs +assert v == 'abc' def bar(): global x @@ -11,13 +18,14 @@ def main(): global x + x = 0 - # This doesn't work: - # foo() - # print x + foo() # affects only globs' x + print x # prints 0, not 'hello' + print globs.x # prints 'hello' - # Only this works: - bar() - print x + bar() # affects this module's x + print x # prints 3 + print globs.x # prints 'hello' main() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |