python basis: graphic python global variables and local variables relevant knowledge

This article describes the graphic python global variables and local variables relevant knowledge, the paper introduced by the sample code is very detailed, with some reference value of learning for all of us to learn or work, a friend in need can refer

Before looking at the global and local variables, we first look at the scope

a = 10
def func():
  b = 20
  def inner():
    c = 30

To this program as an example above: print a, b, c
Here Insert Picture Description
can see only a print 10, b and c are being given, this is the scope of the problem.

We put a .py file to see things a newly created module, a variable defined within a module, such as a, is a global variable of this module, acting on the overall situation, no matter in what can be called,

But b and c are defined inside a function, we called the local variables, can only act within the function. Here Insert Picture Description
We look at a problem

func and inner functions can be called a variable it?
  func function can be called variable c it?
The answer is: a global variable, you can call anywhere
Here Insert Picture Description
but func function may not be called c, this time we put func seen as a whole, then the inner function is defined inside func, c is the inner internal variables Here Insert Picture Description
Summary: variable definitions can be called from anywhere in the global definition, it can only be used within the function inside the function.

Variable lookup order: LEGB

Local scope> enclosing scope> current global module> Python built scope;

Two two methods on a global and local variables in it: global, nonlocal.

Directly on you an example: Here Insert Picture Description
we can see that a locally defined, then we directly call a'll get an error

Only after the definition of call will be printed, and the value of a global variable will not change Here Insert Picture Description
if I want to change the value of a local namespace how to do it? This time is necessary to use a global Here Insert Picture Description
, we can see the value of a global variable has been modified to be 15.

The following is nonlocal: Here Insert Picture Description
We see this when printing a, b, when wrong, the error message is no definition b

But a no error, this is because a global variables, and b is a local variable, so there is such a mistake, correct as follows: Here Insert Picture Description
Because b is a local variable, so before calling with nonlocal statement, then you can call.

Namespaces

Namespace Classification
global namespace (Global): When creating each module loaded for execution, the variable record defined in the module, the module including a function defined in the class, introduction of other modules, module-level variables and constants.
Local namespace (Local): Each function has a name space, a record of all the variables defined in the function, the parameters including local variables function, internally defined.
Built-in namespace (Built-in): any module can be accessed, placed built-in functions and exceptions (such as: input, print, str, list , tuple ...).
Loading sequence namespace

Built-in namespace (before running load) a> global namespace (program execution: from top to bottom loading) a> local namespace (program execution: when the call load)

The value of the order of namespace

Local calls: a local namespace> a global namespace> Built namespace

In a global call: a global namespace> Built namespace

In summary, in the search for variables range from small, to large layer by layer to find the range.
Thank you very much to read
in college chose the self-python, found that eating a working computer basic bad loss, it is not education

No way to do, can only be acquired to make up, then opened his own counter-attack in the road outside the coding, continuous learning python core knowledge, deep

The study of basic computer knowledge, organized, I put our learning Python buckle qun: 774711191, if you are unwilling to mediocrity

, Then with me outside of coding, growing it!

In fact, there is not only technical, more technical stuff than those, for example, such as

He made a fine programmer, rather than "Cock wire", the programmer itself is a noble presence, ah, is not it? Click to join
] you want to want to be a noble person, come on!
@ This article comes from public number: csdn2299, like the number of programmers can focus on public institutions

Published 16 original articles · won praise 0 · Views 6385

Guess you like

Origin blog.csdn.net/haoxuan05/article/details/105301959