当前位置:网站首页>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 global
Second, 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↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
边栏推荐
猜你喜欢
MySQL: What MySQL optimizations have you done?
实例044:矩阵相加
Open3D 泊松盘网格采样
宝塔服务器PHP+mysql网页URL跳转问题
【红队】ATT&CK - 自启动 - 利用LSA身份验证包自启动机制
2022强网杯 Quals Reverse 部分writeup
《GB39707-2020》PDF download
[Kali Security Penetration Testing Practice Course] Chapter 8 Web Penetration
[Semantic Segmentation] 2022-HRViT CVPR
What makes training multi-modal classification networks hard?
随机推荐
OpenCV图像处理学习一,加载显示修改保存图像相关函数
2022.8.8考试清洁工老马(sweeper)题解
[Red Team] ATT&CK - Auto Start - Registry Run Key, Startup Folder
数据挖掘和数据仓库之间的区别
Error state based Kalman filter ESKF
论旅行之收获
GDB之指令基础参数
FusionCompute产品介绍
第二十一章 源代码文件 REST API 参考(三)
实例044:矩阵相加
2022.8.8 Exam Travel Summary
控制台中查看莫格命令的详细信息
Anchor_generators.py analysis of MMDetection framework
【二叉树-简单】112. 路径总和
[Kali Security Penetration Testing Practice Course] Chapter 7 Privilege Escalation
what is a microcontroller or mcu
数据库治理利器:动态读写分离
【二叉树-中等】1104. 二叉树寻路
OpenCV图像处理学习二,图像掩膜处理
[Kali Security Penetration Testing Practice Tutorial] Chapter 6 Password Attack