LEGB order of Python function scopes

高洛峰
Release: 2017-03-02 16:47:00
Original
1223 people have browsed it

This article introduces the search order of Python function scope for your reference. The specific content is as follows

1. What is LEGB?
L:local function internal scope
E:enclosing function internal scope and embedded function Inter
G:global global scope
B:build-in built-in scope

2.LEGBWhat is it used for?
Why do we have to introduce this? Or what is their function?
The reason is because when we learn Python functions, we often encounter many domain problems, all variables, internal Variables, internally embedded functions, etc. How does Python find them? And in what order does Python search? Here is a sequence explanation

3. What is the sequence
Like the name, Python’s search in functions is divided into 4 types, called LEGB, which is exactly according to Search in this order.

First, it is local, first search inside the function
Then, it is enclosing, and then search between the inside of the function and the embedded function (referring to defining a function again inside the function)
Secondly, it is global , search global
Finally, it is build-in, built-in scope

4. Example
ex1

passline = 60

def func(val):
  if val >= passline:
    print('pass')
  else:
    print('failed')

func(89)  

'''''''''''' 
pass
[Finished in 0.2s] 
''''''''''''
Copy after login

The Python function first searches for local. There is no definition of passline in the local variable scope. Then it is found that there is no embedded function inside the function. At this time, Python starts to search for global. The definition of passline is found globally and called.

ex2

##

def Max(val1, val2):
  return max(val1, val2)

print(Max(90, 100))

'''''''''
100
[Finished in 0.1s]
'''''''''
Copy after login

Max function directly calls another function, The called max() (note that the capitalization of the two functions is different), this function has not been defined, but it belongs to the fourth type we mentioned above, which is a build-in function. It is a function in the python standard library and is built-in Yes, it can be called directly. The last step will be to find here

Regarding the second type, it is an embedded function. Even if a function is defined again inside the function, it will first search whether there is a definition in the local function, and then search inside the function. Is there any definition in the embedded function? This type has a special term called closure. Closure has been introduced in some previous articles. I hope you will read it.

The above is the entire content of this article, I hope it will be helpful to everyone's study.

For more articles related to the LEGB order of Python function scope, please pay attention to the PHP Chinese website!



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!