Python-global Global Variables

Source: Internet
Author: User

When you define variables inside a function, they have nothing to do with other variables that have the same name outside the function, that is, the variable name is local to the function, which is called the scope of the variable, as an example:

def func_local (x):    print ' x is ', x    x = 2    "print ' chanaged local x to ', XX = 50func_local (x)" print ' x is still ', X

Execution Result:

X is 50Chanaged local x to 2x is still 50

If you want to change the value of a variable outside of a function, use the global statement to complete

Def func_global ():    global y    print ' y is ', y    y =    [print ' Changed local y to ', yy = 10func_global () print ' Value of Y is ', y

Def func_global ():    global y    print ' y is ', y    y =    [print ' Changed local y to ', yy = 10func_global () print ' Value of Y is ', y

Execution Result:

Y is 10Changed local y to 50Value of y is 50

Y is 10Changed local y to 50Value of y is 50

If the function parameter is a list, set, dict variable parameter, changing the parameter within the function causes the parameter to change, for example:

def func_local (x):    print ' x is ', x    x.append (Ten)    print ' chanaged local x to ', XX = range (6) func_local (x) print ' X is ', X

def func_local (x):    print ' x is ', x    x.append (Ten)    print ' chanaged local x to ', XX = range (6) func_local (x) print ' x is ', X

Execution results

X is [0, 1, 2, 3, 4, 5]chanaged local x to [0, 1, 2, 3, 4, 5, 10]x are [0, 1, 2, 3, 4, 5, 10]

X is [0, 1, 2, 3, 4, 5]chanaged local x to [0, 1, 2, 3, 4, 5, 10]x are [0, 1, 2, 3, 4, 5, 10]
def func_local (x):    print ' x is ', x    x.add (Ten)    print ' chanaged local x to ', XX = set (range (6)) func_local (x) print ' x is ', X

def func_local (x):    print ' x is ', x    x.add (Ten)    print ' chanaged local x to ', XX = set (range (6)) func_local (x) print ' x is ', X

Execution Result:

X is set ([0, 1, 2, 3, 4, 5]) chanaged local x to set ([0, 1, 2, 3, 4, 5, ten]) x is set ([0, 1, 2, 3, 4, 5, 10])

X is set ([0, 1, 2, 3, 4, 5]) chanaged local x to set ([0, 1, 2, 3, 4, 5, ten]) x is set ([0, 1, 2, 3, 4, 5, 10])
def func_local (x):    print ' x is ', x    x[' x '] = 2    print ' chanaged local x to ', XX = Dict ([' X ', 1 '), (' Y ', 2)]) func_l Ocal (x) print ' x is ', X

def func_local (x):    print ' x is ', x    x[' x '] = 2    print ' chanaged local x to ', XX = Dict ([' X ', 1 '), (' Y ', 2)]) func_l Ocal (x) print ' x is ', X

Execution Result:

X is {' Y ': 2, ' x ': 1}chanaged local x to {' Y ': 2, ' x ': 2}x is {' Y ': 2, ' X ': 2}

X is {' Y ': 2, ' x ': 1}chanaged local x to {' Y ': 2, ' x ': 2}x is {' Y ': 2, ' X ': 2}

def func_local (x):    print ' x is ', x    x = (4, 5, 6)    print ' chanaged local x to ', XX = (all in A,) func_local (x) print ' X is ', X
def func_local (x):    print ' x is ', x    x = (4, 5, 6)    print ' chanaged local x to ', XX = (all in A,) func_local (x) print ' X is ', X

Execution results

X is (1, 2, 3) chanaged local x to (4, 5, 6) x is (1, 2, 3)
X is (1, 2, 3) chanaged local x to (4, 5, 6) x is (1, 2, 3)

If a variable parameter such as list, set, Dict is passed, the parameter is changed inside the function, the parameter itself changes, and the tuple and Str are unchanged.

Python-global Global Variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.