Detailed explanation of the LEGB sequence of the Python function scope, detailed explanation of the python function legb

Source: Internet
Author: User
Tags types of functions

Detailed explanation of the LEGB sequence of the Python function scope, detailed explanation of the python function legb

This article introduces the search sequence of Python function scopes for your reference. The specific content is as follows:

1. What is LEGB?
L:Local function internal scope
E:Between the enclosing function and the embedded Function
G:Global scope
B:Build-in Built-in Scope

2.LEGBWhat is it?
Why should we introduce this? Or what are their functions?
The reason is that when we are learning Python functions, we often encounter many problems with definition domains, all variables, internal variables, built-in functions, and so on. How do we find Python? And what sequence does Python search? Here is a description of the order.

3. What is the order?
Similar to the name, there are four types of functions in Python, which are called LEGB.

First, it is local. First, find the Function
Then, enclosing is used to find the internal and embedded functions of the function (that is, define a function again within the function)
Second, global: Search for the global
Finally, build-in, built-in Scope

4. Examples
Ex1

passline = 60def func(val):  if val >= passline:    print('pass')  else:    print('failed')func(89)  '''''''''''' pass[Finished in 0.2s] ''''''''''''

The Python function first looks for local and does not have the definition of passline in the scope of local variables. Then it finds that there is no built-in function in the function. In this case, Python starts to look for global and finds the definition of passline in the global query, called.

Ex2

def Max(val1, val2):  return max(val1, val2)print(Max(90, 100))'''''''''100[Finished in 0.1s]'''''''''

The Max function calls another function directly. The max () function is called (note that the case of the two functions is different). This function is not defined, but it belongs to the fourth one, the build-in function is a function built in the python standard library and can be called directly. The last step is found here.

For the second type, it is an embedded function. Even if a function is defined again in the function, the system first looks for the definition in the local function, and then finds whether the built-in function in the function has any definition, this is a special term called closure, which has been introduced in some previous articles. I hope you can read it.

The above is all the content of this article, hoping to help you learn.

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.