Python Beginner's note--namespace

Source: Internet
Author: User

For beginners, the concept of closures has always been difficult to understand.

When I was studying, I looked for so much information on the Internet, still smattering.

Until later I read this article: http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/

This is a very good article, easy to tell you the end of the closure, the adorner is what things.

e-text, but close? It's okay, I'll use my own understanding to talk to you about namespaces, closures, and decorators.

Premise:

The premise of reading this article is that you want to have a basic understanding of Python, if you are interested, but do not understand Python, I recommend you to read the following tutorial:

Www.liaoxuefeng.com

At the same time, the public number: Easypython, Chinese name: Speaking of Python sharing, is also a very good tutorial, it is recommended to have some basic students to try.

The words of the Python combat plan: Http://study.163.com/course/introduction/1002794001.htm

Begin:

Name space

The concept of namespaces (namespace), sometimes referred to as "scopes", is involved in most of the closures.

What is a namespace/scope?

These two concepts, which are used to describe the properties of a variable, are meant to be literally: "Where the variable takes the name/the field where the variable works".

We use examples to illustrate:

def func ():     = 1

Simple function, this function defines a variable called X, and gives it a value: 1

So, what if we print this variable? (I use annotations to represent the output)

def func ():     = 1    print(x) func ()#  1

Sounds like a total nonsense! Print (x) does not display 1, does it show anything else?

Well, let's change the case, what if we print (x) out of the function body?

def func ():     = 1Print(x)#  Traceback after a big pile I omitted ... # nameerror:name ' x ' is not defined

Reported an error, the type of the error is name error, the name is wrong, the error is the details: x is not defined.

See here, careful friend should know I say so much nonsense is what mean!

x is defined inside the function body, its "scope" is only the function of the body to use!

You want to print out x? Want to add x 1? Want to multiply X by 2? Sorry, please go inside the function body, outside, Python does not know it.

Speaking of Python does not know it, in fact, refers to the "Python interpreter" do not know it, the interpreter is not much explanation, you can simply understand the Python program for your computer.

Okay, well, Python doesn't know it, so what if I'm in a different situation? I'll swap two statements for a position?

x = 1def  func ():    Print(x) func ()#  1

Hey? You're a liar, don't you say Python doesn't know it? Now x is not defined in the function body (in fact there is no definition in the function body)

How do you know it again?

Ask this question, you have to say a Python program, read a variable method.

Simply put, four words: from the inside out.

Python first checks the inside of the function body:

1. I have found, print!

2. I can not find, I went to look outside, found, print!

3. I can't find it, I'll find it outside, I still can't find it! Nameerror: I can't find the variable, don't you define it?

Look, that's the order.

So where's the X-variable you can't find?

For example, this program:

def func ():     = 1print(x)

What about X? Where did X go?

The answer is that X is "recycled" by the Python interpreter, and so-called recycling, you can simply interpret it as deleted and killed.

A concept is introduced here, which is the "Time to live" of a variable.

I wrote it and you know it (for the convenience of marking the end of the function body, I wrote a return statement that returns the string ' Done '):

def func ():         = 1      #  x Survival time starts, in x=1 this line below operation X can!         return'done'  #  function body ends, X is killed by Python. 

The meaning of the code is as follows:

The survival time of a variable within a function, starting at the moment it is defined, until the function body ends.

We can determine what variables are "alive" within the function body by a handy python-given method, which is locals ()

Let's look at a code:

def func ():     ' oops '   ' We're   still alive '             Print (Locals ()) func () # {' A ': ' Oops ', ' C ': ' Still alive ', ' B ': ' we '}

Look, locals () is actually a dictionary, he lists all the "variables: variable values" such as "key-value pairs", and the dictionary is unordered.

Locals () All of the current "Alive" variables, let's look at another example:

def func ():     ' oops '   ' We're   still alive '             Print (Locals ())     ' I'm not even born . ' func () # {' B ': ' We ', ' C ': ' Still alive ', ' a ': ' Oops '}


Look, locals () there is no variable D what's the matter, because D was "not born" at that time.

What about the external variables?

x = 1def  func ():    ' internal '    print(Locals ())     Print (x) func () # {' A ': ' Internal '} # 1


See, there is only one internal variable, locals also print out, but X is still normal to print.

Because Python can not find inside, so went outside to look for, outside the variable, may use Globals () to check, but this is very long ...

x = 1def  func ():    ' internal '    print(Locals ())     Print (Globals ()) func () # {' A ': ' Internal '} #  .... (A lot of me will not paste it) ..... ' X ': 1}

The concept of a namespace/scope is like this, which determines where the Python program "goes" to find the variable.

Python Beginner's note--namespace

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.