Python Function Local variable usage instance analysis, python instance analysis

Source: Internet
Author: User

Python Function Local variable usage instance analysis, python instance analysis

This example describes how to use local variables of python functions. Share it with you for your reference. The specific analysis is as follows:

When you declare variables in the function definition, they have no relationship with other variables with the same name outside the function, that is, the variable name is local for the function. This is called the scope of a variable. The scope of all variables is the block they are defined, starting from the point where their names are defined.

I. Use local variables

Example:

#!/usr/bin/python# Filename: func_local.pydef func(x):  print 'x is', x  x = 2  print 'Changed local x to', xx = 50func(x)print 'x is still', x

Output:

$ python func_local.pyx is 50Changed local x to 2x is still 50

Working principle:

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.

Ii. 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 of using global statements:

#!/usr/bin/python# Filename: func_global.pydef func():  global x  print 'x is', x  x = 2  print 'Changed local x to', xx = 50func()print 'Value of x is', x

Output:

$ python func_global.pyx is 50Changed global x to 2Value of x is 2

Working principle:

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.

I hope this article will help you with Python programming.

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.