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

How to use nonlocal keyword and global keyword in Python

2025-03-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the nonlocal keyword and global keyword in Python, I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

The order in which python references variables: current scope local variables-> outer scope variables-> global variables in the current module-> python built-in variables

1.nonlocal

The nonlocal keyword is used to use outer (non-global) variables in functions or other scopes.

First of all: make it clear that the nonlocal keyword is defined in the closure.

Look at the following code:

X = 0def outer (): X = 1 def inner (): X = 2 print ("inner:", x) inner () print ("outer:", x) outer () print ("global:", x)

Results:

# inner: 2

# outer: 1

# global: 0

Now, add the nonlocal keyword to the closure to declare:

X = 0def outer (): X = 1 def inner (): nonlocal x x = 2 print ("inner:", x) inner () print ("outer:", x) outer () print ("global:", x)

Results:

# inner: 2

# outer: 2

# global: 0

See the difference? This is a function in which another function is nested. When using nonlocal, you declare that the variable is not just in the nested function inner ()

It works, but it works in the whole big function.

2.global

The global keyword is used to use global variables in functions or other local scopes. But you don't have to use the global keyword if you don't modify the global variable.

Again, look at an example:

X = 0def outer (): X = 1 def inner (): global x x = 2 print ("inner:", x) inner () print ("outer:", x) outer () print ("global:", x)

Results:

# inner: 2

# outer: 1

# global: 2

Global works on variables in the entire environment, not on variables of function classes.

The above is all the content of this article entitled "how to use nonlocal keywords and global keywords in Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, 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: 0

*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