<dd id="sga4y"><optgroup id="sga4y"></optgroup></dd>
<nav id="sga4y"><code id="sga4y"></code></nav>
  • <optgroup id="sga4y"></optgroup>
    ???? Python ???
    ??7?? ????
    ???? ??????? ????

    ???????

    ?????????????????????????????????????????????????????????????ʦ????????????????????????? ??? ????????????? ?????? ???????????????????????????????????????????????????

    ??t??????

    ??7.3 ??t??????

    #!/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??????????????

    ???global???

    ??????????????????????????????????????????Python?????????????????????? ??? ??????????global?????????????????global????????????????????????????????

    ?????????????????????????????????????????????????????????????????????????????????????t???????????????????????????????????????????????????????global???????????????????????????E????

    ??7.4 ???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??


    ???? ????? ????
    ?????? ??? ???????
    ?????
    @ssv 97av