Python 3.2 Official Document translations: scopes and Namespace instances

Source: Internet
Author: User

The following examples are primarily used to demonstrate how different scopes and namespaces are referenced, and how the keyword global and nonlocalru affect the binding of variables.

The results of the instance run are:

After the local assignment:test spam

After nonlocal assignment:nonlocal spam

After global assignment:nonlocal spam

In global scope:global spam

Note that local assignment (which is the default) cannot change the binding value of the spam in the Scope_test method. But a nonlocal assignment can change its value. Global variable assignments can only change values at the same level as the module.

You can see that the spam binding value is not given before the global variable is assigned a value.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/

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.