当前位置:网站首页>Instance 042: Variable scope
Instance 042: Variable scope
2022-08-10 03:32:00 【lazily】
Title:Learn how to use auto to define variables.
Program Analysis: Variable scope in python.
I. Problem solving ideas:
In C, auto automatically stores the type. Generally, we rarely declare variables as auto type in the program.Because variables in code blocks are of this type by default, variables of this type are stored on the stack,That is to say, such automatic variables will only be created when the program executes these code blocks, and the automatic variables will be released after the execution of the code block.auto int num = 1That is to say, only then execute this line of codeThe num in the auto variable will exist. After this line of code is executed, the auto variable will be automatically released.We know that Python does not need to declare variables, once the variable is initialized, the type and value are determined.Determines the type of the variable based on the value type.In Python, the auto keyword does not exist# import keyword## print(keyword.kwlist)# print(keyword.iskeyword('auto'))If you try to enter auto in pycharm, the editor will probably prompt you to enter autopep8, don't think this is how auto is written in pythonUse autopep8 to automatically typeset Python code to the PEP 8 specificationautopep8 is an open source command line tool that automatically formats Python code into PEP8 style.autopep8 uses the pycodestyle tool to determine which parts of the code need to be formatted, which fixes most of the typography issues reported by the pycodestyle tool.autopep8 itself is also a tool written in Python language, we can install it directly using pipWithout the auto keyword, you can use variable scope as an exampleIn python, the default local variable local global variable uses globalSecond, code:
def my_global():global gg = 99print('my_global', g)n = 11def my_nonlocal():# nonlocal is generally used in closure functions, variables declared by nonlocal are in the upper-level local scope, not globally definednonlocal nn = 22my_nonlocal()print('n', n)my_global()print('g', g)Three, running results

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Lazy laughter and sincerityInvite you to click below to learn and discuss together↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
边栏推荐
猜你喜欢

storage of data in memory

实例047:函数交换变量
![[Kali Security Penetration Testing Practice Tutorial] Chapter 6 Password Attack](/img/ac/e944d81afc741c38dc775d71dc9014.png)
[Kali Security Penetration Testing Practice Tutorial] Chapter 6 Password Attack

微生物是如何影响身体健康的

实例042:变量作用域

Example 045: Summation

量化交易策略介绍及应用市值中性化选股

The Evolutionary History of the "Double Gun" Trojan Horse Virus

Deep Learning (5) CNN Convolutional Neural Network

【二叉树-困难】124. 二叉树中的最大路径和
随机推荐
T5:Text-toText Transfer Transformer
【红队】ATT&CK - 自启动 - 注册表运行键、启动文件夹
实例042:变量作用域
第二十一章 源代码文件 REST API 参考(三)
数据在内存中的存储
实例043:作用域、类的方法与变量
FusionConpute虚拟机的发放与管理
2022.8.9考试平衡的余数--1000题解
2022.8.9考试立方和--1100题解
实例044:矩阵相加
深度学习(五) CNN卷积神经网络
2022.8.9 Remainder of Exam Balance--1000 Question Solutions
On the Harvest of Travel
【图像分类】2022-ConvMixer ICLR
Open3D 网格均匀采样
【二叉树-中等】1379. 找出克隆二叉树中的相同节点
2022.8.8 Exam Travel Summary
HackTheBox——Beep
推荐几款好用的MySQL开源客户端,建议收藏
Arcgis进阶篇(1)——安装Arcgis Enterprise,创建sde库