Variable scope and pythonscope in Python

Source: Internet
Author: User
Tags variable scope

Variable scope and pythonscope in Python

Http://www.crifan.com/summary_python_variable_effective_scope/

 

Explain the scope of variables in python

Example:

1. Code version

1 #! /Usr/bin/python 2 #-*-coding: UTF-8-*-3 "4 bytes 5 Function: 6 [arrangement] in Python: self and init _ meaning + why should there be self and _ init _ 7 http://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them 8 9 Author: Crifan10 Verison: 2012-11-2711 listen 12 "13 14 # Note: The global variable name here is written as name, which is only used for demonstration. 15 # actually, a good programming style, it should be written as a name such as gName to indicate that the variable is the Global variable 16 name = "whole global name"; 17 18 class Person: 19 name = "class global name" 20 21 def _ init _ (self, newPersionName): 22 # self. name = newPersionName; 23 24 # self. name25 # The name here is still the local variable name26 # although the value is assigned here, it is not used later. It is a wasted local variable name27 name = newPersionName; 28 29 def sayYourName (self): 30 # here, the reason why: 31 # AttributeError: Person instance has no attribute 'name' 32 # That's because, although the corresponding name variable is not initialized in _ init _ in the current instance self, there is no corresponding name variable 33 in instance self # But because the class Person corresponding to the instance, there is a corresponding name variable, so it is also 34 # corresponding to the code that can be normally executed, here self. name, which is actually Person. name35 print 'My name is % s' % (self. name); #-> class global name36 print 'name within class Person is actually the global name: % s' % (name ); #-> whole global name37 print "only access Person's name via Person. name = % s "% (Person. name); #-> class global name38 39 def selfAndInitDemo (): 40 persionInstance = Person ("crifan"); 41 persionInstance. sayYourName (); 42 print "whole global name is % s" % (name ); #-> whole global name43 44 ################################# ######################################## ###### 45 if _ name __= = "_ main __": 46 selfAndInitDemo ();

 

2. graphic version

 

3. Text Version

3.1 Global variables)

Both common functions and Class classes can be referenced directly by corresponding variable names.

3.2 local variables)

For functions including Class functions, common variables are automatic temporary variables.

 

4. Appendix: Variable priority

The most important difference between the scope of common local variables and global variables is:

Internal variables have a higher priority than external variables.

That is, (in the function), the priority of local variables is greater than (external) global variables.

In other words:

A. If A variable with the same name as the global variable appears in the function, it is A local variable;

B. If no local variable with the same name exists in the function, the corresponding variable is a global variable.

The priority relationship of this variable is applicable not only to python but also to almost all languages, such as C/C ++/C #.

Therefore, the name in _ init _ in the above example is not a global variable:

Name = "whole global name ";

The name is a local variable;

Here, it is specific to Python (and, or other special) languages and can be directly used without declaring variables.

That is, the local variable name is not declared, but it is directly used:

Name = newPersionName;

At initialization, set it to the corresponding name. Here it is "crifan.

 

5. Class variables

Class variables, within the scope of the class, can only pass

ClassName. PropertyName (or ClassName. VariableName) to access the PropertyName variable of the current class ClassNamer

In this example, the variable name in the "Person" class is removed through "Person. name ".

 

6. Instance variables

In theory, the variables in the example are accessed using InstanceObject. PropertyName.

Here, the default syntax in Python defines the first parameter name of a function in the class as an Instance variable and the name is self, so it becomes:

Self. PropertyName

To access the Instance variable in the functions of the above class, you can use self. name to access the name variable in the Person class example.

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.