Basic Python Basics Tutorial Local variables and global variables for Python

Source: Internet
Author: User
Tags define function

Local variables

To talk about the technical premise of local variables and global variables is to have a certain understanding of the function, we first here briefly explain the Python function, Python has its own function can also use the third-party toolkit external reference functions, Sometimes the user can self-develop some function-specific functions that we call a custom function. How can users define their own functions? The custom function syntax structure defined by Python is as follows:

def function_name (Parameters): (tab) statement1 (tab) statement2 (tab) statement3 (tab) etc.

Line 1th is the parameter that defines the function name and function, notice that the function has a colon after the right parenthesis! The 2–5 line is a function statement block. It is important to note that each statement in the name of the function is indented with the TAB key, otherwise it is considered to be a statement in a statement block that is not the function, but rather a statement of a program that is sibling to the function. Instead of enclosing the block of statements with curly braces, the Python function body uses tab to distinguish whether a statement is a function or a function. The function has no return value type, and there is no data type description for the row parameter.

Custom functions can be called by other programs or statements elsewhere, as in C, and it is not necessary to use the DEF keyword when invoking a custom function. Let's illustrate.

#define FUNCTION:ADDDEF Add (x, y): z = x + y return z#define main functiondef main (): a = b = 13#funct Ion ' add ' Calledc = Add (A, b) print C#programme entrymain () print ' end! '

The program code executes from line 13th, and the Add and main two functions are defined separately from line 2nd to line 11th. The Add function has two parameters x and Y, and the main function invisible parameter also has no return value. The program starts from line 13th and jumps to the 7th, 8, and 10th lines of the custom main function, and when the program executes to the 10th line, it jumps to the 3rd line of the Add function, the 4th row, and returns to the calling function main, which returns to the calling function main function when it finishes executing. At the same time, the return value will be assigned to the 10th line of the equal sign to the left of the C variable, next to the 11th line, print the sum of results, after the execution of 11 lines, the main is completed after the full execution of the next line of 13th line 14th line to print out the string ' end! ', to the end of the program.

With the basic concept of the function, let's talk about the local variables of the python variables. As the name implies local variables must live in a local area, what is called local variables? Variables defined in the function body are called local variables, so we can quickly get the definition of a global variable, which is the global variable when defining a variable outside the body of the function. Local variables can only be used by this function, and global variables may be used by all statements.

Def f1 (): x = Print xdef f2 (): y = Print ydef f3 (): Print x print Ydef main (): F1 () F2 () %F3 () main () print ' end! '

At this point the 13th line of code is commented out, if you open the 13th row error is as follows:

>>>1213traceback (most recent): File "f:/python27/t2.py", line <module> Main () file "  f:/python27/t2.py ", line +, in main F3 () File" f:/python27/t2.py ", line 8, in F3 print Xnameerror:global name ' x ' is not defined>>>

The error is that the main function found a problem with the F3 function when calling the function F3 (line 8th) x in "Print X" is not defined. I guess the designer wanted to print the X-F1 definition to print out 12, right? But x is defined in the custom function F1, X at this time is the local variable can not be in addition to the F1 function or statement use, so error. Because local variables are defined in the function body, multiple functions can use the same variable name as their own variable in the respective function body.

Global variables

Def printlocalx ():    x = 12    print  ' f1  local x =  ',         print xdef printlocaly ():     y = 13    print  ' f2 local y =  ' ,        print ydef readglobal ():         print  ' f3 read global x =  ',         print x        print  ' f3 read  global y =  ',         print ydef modifyglobal ( ):    global x        print  ' f4  Write x = -1 '     x = -1def main ():     Printlocalx () &NBSP;&NBSP;&NBsp; printlocaly ()     readglobal ()     modifyglobal () x =  200y = 100main () print  ' after modified global x =  ',print  xprint  ' end! '

At this point, because the program defines the global variables x and Y, so the Readglobal function of the two printed statements are not reported syntax error. Global variables can be used by any child functions, and in the Readglobal function (lines 11th and 13th) read the values of the x and Y variables, no problem. In the Modifyglobal function body (line 17th) assigns X to 1, if the custom function body modifies the global variable, first declare the x as a global variable as in line 15th so that the global x variable can be modified. If you remove rows 15th and 16, the X of line 17th is considered a local variable of the Modifyglobal function.

The program execution results are as follows:

F1 local x = 12f2 local y = 13f3 Read Global x = 200f3 Read global y = 200f4 write x = -1after F4 modified Global x = -1end!


Basic Python Basics Tutorial Local variables and global variables for Python

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.