当前位置:网站首页>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↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
边栏推荐
猜你喜欢
随机推荐
微生物是如何影响身体健康的
What is a Cross-Site Request Forgery (CSRF) attack?How to defend?
从滑动标尺模型看企业网络安全能力评估与建设
剑指offer专项突击版第25天
Research on IC enterprises
2022.8.8 Exam Travel Summary
状态压缩小经验
数据库治理利器:动态读写分离
OpenCV图像处理学习三,Mat对象构造函数与常用方法
Open3D 网格均匀采样
实例047:函数交换变量
Robust Real-time LiDAR-inertial Initialization(实时鲁棒的LiDAR惯性初始化)论文学习
【图像分类】2022-CycleMLP ICLR
liunx PS1 settings
State compression small experience
Open3D 泊松盘网格采样
如何编写一份优质的测试用例?
网页挖矿溯源?浏览器浏览历史查看工具Browsinghistoryview
【二叉树-中等】1261. 在受污染的二叉树中查找元素
Screen 拆分屏幕









