Introduction to local variables of Python Functions

Source: Internet
Author: User

If you have any questions about Python functions, for example, if you have doubts about the local variables of Python functions, you can read our article. I hope you will gain some benefits after reading our article, the following is a detailed description of the article.

Local variable

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.

Use a local variable example 7.3 using a Python Function

How to output Python Functions

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, I

 
 
  1. #!/usr/bin/python  
  2. # Filename: func_local.py  
  3. def func(x):  
  4. print 'x is', x  
  5. x = 2 
  6. print 'Changed local x to', x  
  7. x = 50 
  8. func(x)  
  9. print 'x is still', x   
  10.  

Value 2 is assigned 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.

The above section describes the Python functions in practical application.

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.