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

What is the code snippet that python can change the scope of a variable

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

Share

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

This article is to share with you about python can change the scope of variables of the code segment is, the editor thinks it is very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

Several concepts:

The code segments that python can change the scope of variables are def, class, lamda.

The variable search path is: local variable-> global variable

The code segments that python can change the scope of variables are def, class, lamda.

[python] view

Plaincopyprint?

Def scopetest ():

Localvar=6

Print (localvar)

Scopetest ()

# print (localvar) # removing comments will cause an error here, because localvar is a local variable

If/elif/else 、 try/except/finally 、 for/while

[python] view

Plaincopyprint?

While True:

Newvar=8

Print (newvar)

Break

Print (newvar)

Try:

Newlocal=7

Raise Exception

Except:

Print (newlocal) # can be used directly.

Output: 8 8 7

It can be seen that variables are defined in this keyword, and their scope is consistent with the outside, which is a little different from the concept of scope in Java.

The variable search path is: local variable-> global variable

[python] view

Plaincopyprint?

Def scopetest ():

Var=6

Print (var) #

Var=5

Print (var)

Scopetest ()

Print (var)

Output: 5 6 5

Here var first searches for local variables. Var=6 in scopetest () is equivalent to defining a local variable with an assignment of 6. 0. Of course, if you do want to change the value of a global variable, you need the following:

[python] view

Plaincopyprint?

Def scopetest ():

Global var

Var=6

Print (var) #

Var=5

Print (var)

Scopetest ()

Print (var)

Output: 566

Look at another situation like this:

[python] view

Plaincopyprint?

Def scopetest ():

Var=6

Print (var) #

Def innerFunc ():

Print (var) # look here

InnerFunc ()

Var=5

Print (var)

Scopetest ()

Print (var)

Output: 5 6 6 5

Reverse search according to the order of calls, first local variables and then global variables, for example, search for local variables in innerFunc, no, well, look for the scopetest at the next level of the call relationship, find the local variable var=6,OK, and use him.

These are the code snippets in which python can change the scope of variables, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please 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