Python does not return _ local variables and global variables; python does not return

Source: Internet
Author: User

Python does not return _ local variables and global variables; python does not return

Local and global variables

Local variable: The variable defined in the function. The scope is the current function and only applies to the current function.

Global variable: a variable defined at the beginning of the Code. The scope is the whole code and it takes effect for the whole code.

name = 'jwc'def func1(name):    print('my name is %s' %(name))    name = 'gally'    print('now,my name is %s' %(name))func1('jiang')print(name)

Output result:

My name is jiang

Now, my name is gally

Jwc

According to the results of function running, when global variables are the same as local variables, local variables are used in the function and global variables are used outside the function.

If we want to make the local variables affect global variables within the function, we can use gloabl in the function. Let's take a look.

name = 'jwc'def func2():    global name    name = 'gally'    print(name)func2()print(name)

Output result:

Gally

Gally

Let's take a look.

name_info =['jack','jiang','jwc']def func3():    name_info[0] = 'gally'func3()    print(name_info)

Output result:

['Gally ', 'jiang', 'jwc ']

Here we can see that the global variable name_info has changed, but the global variable has not been called in the function. In python, if you only modify the value of an element, such as a list or dictionary, you do not need gloabl. If you want to modify the entire list, you must have gloabl.

name_info=['jack','jiang','jwc']def func4():    name_info = []func4()print(name_info)

Output result:

['Jack', 'jiang ', 'jwc']

name_info=['jack','jiang','jwc']def func5():    gloabl name_info    name_info = []func5()print(name_info)

Output result:

[]

Summary: A local function only acts on this function. A global function acts on the entire code segment. to modify the value of a global variable in the function, use gloabl. If you only modify a value in the list or dictionary, you can modify the global variable without using global.

------- O & M that won't be developed is not a good cook

  

 

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.