0 Basic python-16.1 Scope Quick Start

Source: Internet
Author: User

In this chapter, let's talk about the scope basics.

Python creates, alters, or looks up variables in a so-called namespace, which refers to namespaces

Simply put: The range that this variable can be accessed

Let's give an example that's easier to understand--the function

>>> def Test (): X=1print (x) >>> Xtraceback (most recent):  File ' <pyshell#10> ', line  1, in <module>    xnameerror:name ' x ' isn't defined>>> test.xtraceback (most recent call last):  File "<pyshell#11>", line 1, in <module>    

We define a variable x in the function, but outside of the function, x cannot be called, nor can it be called with test.x, because the scope of x is only in the function test and cannot be called outside.

So: When we call Test (), the print in it is printable, and the outside call is not possible.


Moreover, we define a variable outside the function with the same name as X, which does not conflict with it, because they point to two different objects, even if we define an x in it, it does not conflict, because it is also pointing to the object that is not the same, it needs to be noted here: Because of the Python caching mechanism, at certain times , the x inside the function points to the same object as the x outside.


As we can see from the code, for integers 1 and short string abc, they are cached in Python, the X in Python points to the same object, and for long string a b C, because there is no caching mechanism, it points to different objects, In fact, we have more of a third situation in the project, but we have to pay attention to the 12th situation, because he may bring us some pits, unseen pits, and can not find out the pit, so here we must pay attention to the Python caching mechanism

Three different scopes:

* If the variable is assigned in Def, it is positioned inside the function

>>> def Test (): X=1


* If a variable is assigned a value in a nested Def, it is not local to the nested function



* If you assign a value outside of Def, it is global

>>> x=1


Summary: This chapter provides a brief overview of the basis of the scope, the special circumstances of the scope, and the three scopes of Python


This chapter is here, thank you.

------------------------------------------------------------------

Click to jump 0 basic python-Catalogue




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

0 Basic python-16.1 Scope Quick Start

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.