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 the local scope of python

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

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to use python local scope". Xiaobian shows you the operation process through actual cases. The operation method is simple, fast and practical. I hope this article "how to use python local scope" can help you solve the problem.

description

1. Variables and arguments assigned within a function are locally scoped and belong to local variables. A variable assigned outside a function is global in scope and belongs to the global variable. A variable must be one of these, and cannot be both global and local.

note

Local scopes can access global variables.

Local scopes cannot use variables in other local scopes.

examples

Local scopes cannot use variables def spam() from other local scopes: egg = 99 becon() print(egg) def becon(): egg = 0 ham = 101 spam() print result 99 At the start of the program, the spam() function is called, creating a local scope. The local variable eggs is assigned 99. The bacon() function is then called, creating a second local scope. Multiple local scopes can exist simultaneously. In this new local scope, the local variable ham is assigned 101. The local variable eggs (different from the one in the local scope of spam()) is also created and assigned 0. When bacon() returns, the local scope of this call is destroyed. Program execution continues in the spam() function, which prints out the value of eggs. Because the local scope of the spam() call still exists, the eggs variable is assigned 99. This is the printout of the program. The point is that local variables in one function are completely separated from local variables in other functions. The content of "how to use python local scope" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

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