Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of scope rules in Python function

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the Python function in the scope rule instance analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this Python function in the scope rule instance analysis article will have a harvest, let's take a look at it.

Python is a static scoped language, but it is a dynamic language itself. In Python, the scope of a variable is determined by its position in the code, somewhat similar to the C language, but not exactly the same.

In Python 2.0 and previous versions, Python only supports three scopes, namely, local scope, global scope, and built-in scope; in Python

In 2.2, Python formally introduces a new scope-nested scope; the introduction of nested scope essentially supports closures for Python.

1. Briefly introduce the closure def test (): a = 3B = 4 def stu (): C = 3 return A+B+C return stustu = test () stu

In the above code, the stu method is defined inside the test, and the inner function stu can use the variables of the external function test. This behavior is called closure.

2. In Python, not every code block can introduce a new scope

Not every code block in Python can introduce a new scope, which is very different from C #:

In C:

# includeint main () {if (2 > 0) {int I = 0;} printf ("I =% d", I); return 0;}

In the above code, the if clause introduces a local scope in which the variable I exists but is not visible to the outside, so a subsequent reference to the variable I in the printf function will cause a compilation error.

However, this is not the case in Python:

If True: I = 0print I

In the above code, the if clause does not introduce a local scope, and the variable I is still in the global scope, so the variable I is visible to the next print statement.

In fact, in Python, only modules, classes, and functions introduce new scopes, and other code blocks do not introduce new scopes.

In Python, you don't have to declare a variable before you use it, but it must be bound to an object before you can actually use it; name binding introduces a new variable in the current scope, while masking variables of the same name in the outer scope, no matter where the name binding occurs in the current scope.

> f () Traceback (most recent call last): File ", line 1, in File", line 2, in fNameError: name 'i'is not defined >

An error was reported in the running result:

NameError: name 'i'is not defined

When the program is running, Python first looks for variable I in the local scope of function f and fails, then looks for variable I in global scope and built-in scope, still fails, and finally throws a NameError exception.

> def f (): I = 8. Print (I)... > > f () 8 > print (I) 0 >

The running results show that:

8 and 0

I = 8 is a name binding operation, which introduces a new variable I in the local scope of the function f, shielding the global variable I, so the print statement inside f sees the local variable I, the print statement outside f sees the global variable I.

> I = 0 > > def f ():... Print (I)... I = 0... > > f () Traceback (most recent call last): File ", line 1, in File", line 2, in fUnboundLocalError: local variable 'i'referenced before assignment >

An error was reported in the running result:

UnboundLocalError: local variable 'i'referenced before assignment

In the above, the variable I in function f is a local variable, but when the print statement uses it, it is not yet bound to any object, so an exception is thrown.

3. In Python, name binding introduces new variables in its scope and binds to an object at the same time.

Name binding occurs in the following situations:

Parameter declaration: the parameter declaration introduces a new variable in the local scope of the function

Assignment: initial assignment of a variable introduces a new variable in the current scope, and subsequent assignment rebinds the variable

Class and function definitions: class and function definitions introduce class and function names into the current scope as variables, and the class body and function body will form another scope

Import statement: the import statement introduces new variables in the current scope, usually in the global scope

For statement: the for statement introduces a new variable (loop variable) in the current scope

Except statement: the except statement introduces a new variable (exception object) in the current scope

This is the end of the article on "instance Analysis of scope rules in Python functions". Thank you for reading! I believe you all have a certain understanding of the knowledge of "scope rule instance analysis in Python function". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 287

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report