Scope of variables in Python (variable scope)

Source: Internet
Author: User
Tags variable scope

http://www.crifan.com/summary_python_variable_effective_scope/

Explaining the scope of variables in Python

Example:

1. Code version

1 #!/usr/bin/python2 #-*-coding:utf-8-*-3 """4 -------------------------------------------------------------------------------5 Function:6 "Grooming" python: the Meaning of self and init__ + why you have self and __init__7 Http://www.crifan.com/summary_the_meaning_of_self_and___init___in_python_and_why_need_them8  9 Author:crifanTen verison:2012-11-27 One ------------------------------------------------------------------------------- A """ -   - #Note: The global variable name, written as name, is used only for demonstration purposes. the #in fact, a good programming style should be written in a name such as Gname to indicate that the variable is a global variable -Name ="Whole Global name"; -   - classPerson : +Name ="class Global Name" -   +     def __init__(self, newpersionname): A         #self.name = newpersionname; at           -         #here, no self.name is used -         #and so the name here, is actually still the local variable name -         #Although it is assigned here, it is not used later, it is a wasted local variable name -Name =Newpersionname; -   in     defSayyourname (self): -         #here, the reason is not the same as before: to         #Attributeerror:person instance has no attribute ' name ' +         #that is because, although the current instance of self does not initialize the corresponding name variable in the __init__, there is no corresponding name variable in the instance self -         #However, because the class person corresponding to the instance has a corresponding name variable, it is also possible to execute the code normally. the         #corresponding, the self.name here, is actually person.name *         Print 'My name is%s'% (Self.name);#class Global Name $         Print 'name within class person is actually the global name:%s'% (name);#- Whole global namePanax Notoginseng         Print "Only access person ' s name via person.name=%s"% (Person.name);#class Global Name -   the defSelfandinitdemo (): +Persioninstance = Person ("Crifan"); A persioninstance.sayyourname (); the     Print "whole global name is%s"% (name);#- Whole global name +       - ############################################################################### $ if __name__=="__main__": $Selfandinitdemo ();

2. Graphic version

3. Text version

3.1 Global variables (variable)

In any other common function, or class, you can refer directly to the corresponding variable name.

3.2 Local variables (Automatic variable)

For functions within a function, including classes class, the normal variable is an automatic temporary variable

4, attached: The priority of the variable

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

Internal variables have precedence over external variables

That is, the local variable takes precedence over (external) global variables (within the function)

Other words:

A, if inside the function, there is a variable with the same name as the global variable, it is a local variable;

B, if there is no local variable with the same name in the function, the corresponding variable is a global variable

The precedence of this variable is not only for the Python language, but for almost all languages, such as c/c++/c#, etc.

So, the name in __init__ in the above example is not a global variable:

Name = "Whole global name";

In the name instead of the local variable;

Where, in this case, it belongs to the type of Python (and, or other special) language that is unique and can be used directly without declaring variables.

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

name = Newpersionname;

In the initialization, set to the corresponding name, here is "Crifan".

5. Class variables

class, within the scope of the class, only through the

ClassName.PropertyName (or Classname.variablename) to access the variables of the current class Classnamer PropertyName

In the example, the variable name in the class person class is accessed through Person.name.

6. Example (Instance) variable

The variables in the example are theoretically accessed using Instanceobject.propertyname.

Here, because of the default notation in Python, the first parameter name of a function in a class is defined as a instance variable, and the name is self, so it becomes:

Self. PropertyName

To access the instance variable in a function of the class above, you can access the name variable in the person class example through Self.name.

Scope of variables in 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.