Global variables, variables that can be called globally
Local variables, variables that can be called in a subroutine
' W ' def text (): ' L ' Print (name) text () print (name)
Global variables are called and can be re-assigned, and cannot be re-assigned if not
Text () determines the order of calls
' W ' def text (): Global name ' L ' Print (name) def text2 (): print (name) Text2 () text () print (name)
Global variables are called and can be re-assigned, and cannot be re-assigned if not
' W ' def text (): Global name ' L ' Print (name) def text2 (): print (name) text () Text2 () print (name)
No global cannot be re-assigned, but for mutable types, internal elements can be manipulated
name = ['1','2']def A (): name.append (' 3') print (name) a () print (name)
Global variables are uppercase, local variables are lowercase
Nonlocal refers to the upper level variable
' W ' def ABC (): ' a ' def BCD (): nonlocal name 'b' BCD () Print (name) print (name) ABC () print (name )
Python local variables and global variables DAY15