SoFunction
Updated on 2024-10-29

Python 3.5 Local and Global Variable Scoping Example Analysis

This article is an example of Python 3.5 local and global variable scopes. Shared for your reference, as follows:

1. Local versus global variable definitions:

existVariables defined in subroutines (functions) are called: local variables; in the programVariables defined at the top (at the beginning) are called: global variables

2. Local and global variable scopes:

Local variable scope: the subroutine in which the variable is defined; global variable scope: the entire program.

When a local variable has the same name as a global variable, the local variable acts within the subroutine in which it is defined; the global variable acts elsewhere.

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:ZhengzhengLiu
school = "Qing hua"    # Global variables
def change_name(name):
  school = "Bei da"  # Local variables
  print(school)
  print("before change:",name)
  name = "LIU"  # Local variables, valid only inside the function (the scope of a local variable is the subroutine in which it is defined)
  print("after change:",name)
print(school)
name = "liu"    # Global variables that are valid throughout the program
change_name(name)
print(name)

Run results:

Qing hua
Bei da
before change: liu
after change: LIU
liu

Note: To change a local variable to a global variable inside a function, you need to use theglobalstatement (don't use it this way: multiple calls to the program can lead to confusion in the logic, which is not conducive to debugging).

school = "Qing hua"    # Global variables
def change_name(name):
  print("before change:",name)
  name = "LIU"  # Local variables, valid only inside the function (the scope of a local variable is the subroutine in which it is defined)
  print("after change:",name)
  global school    # Change local variables within functions to global variables declared with global
  school = "Bei da"
name = "liu"    # Global variables that are valid throughout the program
change_name(name)
print(name)
print("school:",school)

Run results:

before change: liu
after change: LIU
liu
school: Bei da

3. In addition to integers and strings.Modification of global variables can be achieved in lists, dictionaries, collections, and classes by modifying local variables in subroutines (subfunctions).

names = ["liu","zhang","wang"]
def chang_name():
  names[0] = "sun"
  print(names)
chang_name()
print(names)

Run results:

['sun', 'zhang', 'wang']
['sun', 'zhang', 'wang']

Note: In Python, theCtrl+? shortcut for multi-line comments

For those interested in Python-related content, check out this site's feature: theSummary of Python function usage tips》、《Python Object-Oriented Programming Introductory and Advanced Tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python string manipulation techniques》、《Summary of Python coding manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.