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

Example Analysis of function Local variables, Global variables and Recursive knowledge points in python

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

Share

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

Editor to share with you python function local variables, global variables, recursive knowledge points of the example analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to understand it!

Function local variable global variable and its scope

# relationship between local variables and global variables and their scope of variables of simple types (int str, etc.) name = "xxx" # variables defined at the first level are called global variables, and their scope starts from the position where the variable is defined to the end of this program: def Print_Name (): print ("name before change:", name) # because name has been defined as a global variable before this function Here the function reads the variable name is the read global variable name "xxx", so the local variable of the same name cannot be defined inside the function, otherwise it is impossible to distinguish whether the variable is a local variable or a global variable # name = "yyy" # this statement will report an error: IndentationError: unindent does not match any outer indentation levelPrint_Name () def Change_Name (): name = "yyy" # the local variable of the same name is first defined inside the function. Then the internal operation of the entire function is a local variable (a temporary variable with the same name as the global variable) print ("name after change is", name) Change_Name () # yyy actually outputs the value of the local variable print ("global name is", name) # xxx, which is accessed here, because this print statement is not inside the Change_Name. # the global variable needs to be forced to be modified inside the function. Use the global keyword def Change_global_Name (): global name print ("name before change is:", name) name = "yyy" Change_global_Name () # xxx the global variable print ("global name is:", name) # yyy before modification. Here, because name is declared as a global variable within the function, it is mandatory to declare global variables as global variables. The whole function can operate on global # complex data structures such as complex data type list collection dictionary if the function is defined outside the function, it is possible to operate global variables without global declaration. There are no local variables: names = ['111,' 222, '333'] def Change_Names (): names [0] = "444" print ("in function names:", names) Change_Names () # in function names: ['444', '222', '333'] print ("global names:", names) # in function names: ['44418,' 222,] for complex data types Global variables are directly manipulated within the function and local variables are no longer generated.

Recursion

# other functions can be called inside the function If a function calls itself internally, it is called a recursive function def Half_fun (n): print (n) if n < 2: return n Half_fun (n Half_fun 2) Half_fun (100) # Recursive properties: 1, there must be an explicit end condition 2, the complexity of each recursive call needs to be simplified compared with the last time, recursion will take up a lot of memory and above are "local variables, global variables, Sample Analysis of Recursive knowledge points "all the contents of this article Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to 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