Python Namespace-namespaces

Source: Internet
Author: User

Name space
Namespace namespace is a very central concept for Python, and the entire Python virtual machine runs in a very close connection to the namespace concept.
From the ' assignment ', the assignment statements in Python are a special kind of statement because they affect namespace.
The assignment statement in Python first creates an object, and then assigns the obj ' assignment ' to a name named name (which is understood to be assigned to a variable)
After the assignment statement is executed, one of our relationship between name and object is called ' constraint ', and ' constraint ' is ' namespace '.
In Python, namespaces are implemented through a Pydictobject object.
Take ' class ' as an example, Class A (object): The ' assignment ' statement, you can view the namespace of a by a.__dict__.

Look at an example,
1A = 12         deffunc ():3A = 34             Print('Func-A ='A#15 6         Print('Outer A ='A#27 func ()8         Print('Outer A =', a)9 Ten Output, OneOuter A = 1#2 #1 different results from #2 show that two assignment statements are ' constraints ' created in different namespace AFunc-A = 3#1 -Outer A = 1#The value of the outer variable A is not affected by the assignment statement A = 3 at the #1.
    Within a Python modules module, there may be multiple namespace, and each namespace corresponds to a ' scope ' (an area where a ' constraint ' works-that is, a block of code, called the scope of the constraint).
In the example above, the Def func (): A block of code that sets up a join scope, in which the constraint ' A = 3 ' is in effect. However, the ' outer ' of the function definition statement def is another scope, and the constraint ' a = 1 ' functions.
    def ' A = 3 ' comment out, look at the output,         # a =3,            Outer a =  1            a =              1 a =  1

As you can see, the Python virtual machine searches for the constraint ' a = 1 ' of a in the ' Outer scope ' namespace, and Python supports ' nested scopes '.
    In order to find the object referenced by a given name, it should be found in the namespace that is currently owned by this name. If found, it is the constraint associated with the given name.
Otherwise, continue to the outer namespace and continue to check the outer scope until the outermost nested scope in turn.
If you still do not find the corresponding constraint for name, then raise exception, ' Nameerror:name ' name ' is not defined '






Python Namespace-namespaces

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.