Python variable scope

Source: Internet
Author: User
Tags variable scope

http://blog.csdn.net/lovingprince/article/details/6627555

Several concepts:

  • The code snippet in which Python can change the scope of a variable is Def, class, Lamda.
  • If/elif/else, try/except/finally, For/while are not involved in variable scope changes, which means that the variables in their code block are also accessible externally.
  • Variable search path is: local variable, global variable

    • The code snippet in which Python can change the scope of a variable is Def, class, Lamda.
[Python]View Plaincopyprint?
    1. Def scopetest ():
    2. localvar=6;
    3. print (Localvar)
    4. Scopetest ()
    5. #print (Localvar) #去除注释这里会报错 because Localvar is a local variable
    • If/elif/else, try/except/finally, For/while
[Python]View Plaincopyprint?
    1. while true:   
    2.     newvar=8  
    3.     print (newvar)   
    4.      break;  
    5.   
    6. span class= "keyword" >print (newvar)   
    7.   
    8. TRY:  
    9.     newlocal=7  
    10.     RAISE EXCEPTION  
    11. EXCEPT:  
    12.     print (newlocal) #可以直接使用哦   

Output results: 8 8 7

It is visible that the variables are defined in this keyword, and their scope is consistent with the outside, which is a bit different from the concept of Java scope.

    • Variable search path is: local variable, global variable
[Python]View Plaincopyprint?
    1. Def scopetest ():
    2. var=6;
    3. Print (Var)#
    4. var=5
    5. Print (VAR)
    6. Scopetest ()
    7. Print (VAR)

Output results: 5 6 5

Here Var first searches for local variables, and scopetest () var=6 the equivalent of defining a local variable, assigned a value of 6. Of course if you really want to modify the value of a global variable, you need the following:

[Python]View Plaincopyprint?
    1. Def scopetest ():
    2. Global Var
    3. var=6;
    4. Print (Var)#
    5. var=5
    6. Print (VAR)
    7. Scopetest ()
    8. Print (VAR)

Output results: 5 6 6

Let's look at one such situation:

[Python]View Plaincopyprint?
    1. def scopetest ():   
    2.     var=6;  
    3.      print (var) #  
    4.     def innerfunc ():   
    5. print (var) #look  here  
    6.     innerfunc ()   
    7.       
    8. Var=5   
    9. print (var)   
    10. scopetest ()   
    11. print (var)   

Output results: 5 6 6 5
Reverse search According to the order of the call, first local variables and global variables, such as search for local variables in Innerfunc, no, OK, find the call relationship on the first level scopetest, found the local variable Var=6,ok, use him.

Python variable scope

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.