#在python 3, 請注意s的值並不會共用
# a.py
from b import *
def main():
global s
print(s)
add()
print(s)
s += 6
print(s)
add()
print(s)
if __name__ == "__main__":
main()
# b.py
s =10
def add():
global s
s += 1
print("s=%d"%s)
###
# Execute a.py
###
10
s=11
10
16
s=12
16
#如果要s的值共用,可以如下修改:
# a.py
import b
def main():
print(b.s)
b.add()
print(b.s)
b.s += 6
print(b.s)
b.add()
print(b.s)
if __name__ == "__main__":
main()
# b.py (不變)
s =10
def add():
global s
s += 1
print("s=%d"%s)
###
# Execute a.py
###
10
s=11
11
17
s=18
18
# a.py
from b import *
def main():
global s
print(s)
add()
print(s)
s += 6
print(s)
add()
print(s)
if __name__ == "__main__":
main()
# b.py
s =10
def add():
global s
s += 1
print("s=%d"%s)
###
# Execute a.py
###
10
s=11
10
16
s=12
16
#如果要s的值共用,可以如下修改:
# a.py
import b
def main():
print(b.s)
b.add()
print(b.s)
b.s += 6
print(b.s)
b.add()
print(b.s)
if __name__ == "__main__":
main()
# b.py (不變)
s =10
def add():
global s
s += 1
print("s=%d"%s)
###
# Execute a.py
###
10
s=11
11
17
s=18
18
沒有留言:
張貼留言