python 模块及全局变量


test_sa.py

# -*- coding: utf-8 -*-
"""
User-defined parameters 
"""

global a
a=5

def change_a():
    global a #模块内使用a,必须使用global变量
    a+=3
    print"local a:"+str(a)

def print_a():
    global a #模块内使用a,必须使用global变量
    a='hello'
    print a

new_sa.py (主函数)
import test_sa

test_sa.change_a()
print test_sa.a

test_sa.a+=3
print test_sa.a

test_sa.print_a()
 总结:
1 在模块内:函数内:使用外部的参数需要用global,
2 主函数: 使用模块内的参数需要加模块





posted @ 2017-10-03 20:55  Hello_to_Sa's_world  阅读(1626)  评论(0编辑  收藏  举报