Introduction to python local variables and global variables

Source: Internet
Author: User

What are global variables and local variables?

Outside a function, a piece of code is the variable that is assigned the value at the beginning. It can be referenced by multiple functions. This is the global variable;
The variable name defined in the function can only be referenced inside the function. It cannot be referenced outside the function. The scope of the variable is local, also called local variable;

If the variable names in the function are the same as those outside the function, no conflict will occur. Like the following:

X = 100

Def func ():
X = 55
X = 100 the variable X created by the value assignment statement. The scope is a global variable;

X = 55 the variable X created by the value assignment statement. Its scope is a local variable and can only be used in the function func.

Although the two variables are the same, their scopes are differentiated. Scope can also prevent variable name conflicts in the program to some extent, but it is better to avoid this situation if you are a beginner of python.


Use local variables

Example 7.3 use local variables

#! /Usr/bin/Python
# Filename: func_local.py

Def func (x ):
Print 'X', x
X = 2
Print 'changed local x to ', x

X = 50
Func (x)
Print 'X is still', x

(Source File: code/func_local.py)

Output

$ Python func_local.py
X is 50
Changed local x to 2
X is still 50


How it works

When we use the value of x for the first time in a function, Python uses the value of the form parameter declared by the function.

Next, we assign value 2 to x. X is the local variable of the function. Therefore, when we change the value of x in the function, the x defined in the main block is not affected.

In the last print statement, we prove that the value of x in the main block is indeed not affected.

Use global statements

If you want to assign a value to a variable defined outside the function, you have to tell Python that the variable name is not local, but global. We use the global statement to complete this function. Without a global statement, it is impossible to assign values to variables defined outside the function.

You can use the value defined outside the function (assuming there is no variable with the same name in the function ). However, I do not encourage you to do this, and you should avoid it as much as possible, because this makes the reader of the program unclear where the variable is defined. The global statement clearly indicates that the variable is defined in the block outside.

Example 7.4 use a global statement

#! /Usr/bin/python
# Filename: func_global.py

Def func ():
Global x

Print 'X', x
X = 2
Print 'changed local x to ', x

X = 50
Func ()
Print 'value of x is ', x

(Source File: code/func_global.py)

Output

$ Python func_global.py
X is 50
Changed global x to 2
Value of x is 2

How it works

The global statement is used to declare that x is global. Therefore, when we assign the value to x in the function, this change is also reflected when we use the value of x in the main block.

You can use the same global statement to specify multiple global variables. For example, global x, y, and z.


Summary

1. The scope of the variable is determined by the location where the code is assigned.
2. Variables can be in three different places and correspond to three different scopes:
(1) a variable is assigned a value in the function, and its function scope is located within the function.
(2) when a variable is assigned a value in a nested function, this variable is non-local for this nested function.
(3) assign values to variables outside the function. The scope is the global variable of the entire file.
(4) If you want to assign a value to a global variable in a function, you must declare it with the global keyword.

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.