| ???? Python ??? | ??7?? ???? | |
|---|---|---|
| ???? | ??????? | ???? |
?????????????????????????????????????????????????????????????κι????????????????????????? ??? ????????????? ?????? ?????б???????????????????????飬???????????????????????
#!/usr/bin/python
# Filename: func_local.py
def func(x):
print 'x is', x
x = 2
print 'Changed local x to', x
x = 50
func(x)
print 'x is still', x
????????code/func_local.py??
$ python func_local.py
x is 50
Changed local x to 2
x is still 50
??????У????????????x?? ? ?????Python??ú??????????βε????
??????????????2????x??x???????????????????????????????????x???????????????ж????x???????
????????print????У???????????????е?x??????????????
??????????????????????????????????????????Python?????????????????????? ??? ??????????global?????????????????global????????????????????????????????
?????????????????????????????????????????????????????????????????????????????????????t??????????????????????ó?????????????????????????????????global???????????????????????????鶨????
#!/usr/bin/python
# Filename: func_global.py
def func():
global x
print 'x is', x
x = 2
print 'Changed local x to', x
x = 50
func()
print 'Value of x is', x
????????code/func_global.py??
$ python func_global.py
x is 50
Changed global x to 2
Value of x is 2
global???????????x????????????????????????????????x?????????仯?????????????????????x????????
????????????global?????????????????????global x, y, z??
| ???? | ????? | ???? |
|---|---|---|
| ?????β? | ??? | ??????? |