Variable type
/Variable type
In Python, there are several main variables, namely global variables, local variables, and built-in variables,
Global Variables
/globals Variable
Usually defined inside the module, the uppercase variable name form exists and can be imported by other modules, but there is also a special private variable that starts with a single/double underscore, is also defined within the module, but cannot be imported from the modelname import *.
Locals
/local Variable
A local variable is usually defined inside a function, and the variable name exists in lowercase, only in the local scope of the function.
built-in variables
/built-in Variable
The built-in variables are some of the existing variables, which can be viewed through VARs (), often with __name__ and so on.
Variable Example
/Variable Example
Here is a piece of code to illustrate the differences between these variables,
1 Print(VARs ())#Show built_in Variables2 3Global_variable ="This is global variable"4_private_variable ="This is private variable"5 6 defcall_local ():7local_variable ="This is local variable"8 Print(local_variable)9 Ten defCall_global (): One Globalglobal_variable A Print(global_variable) - - defcall_private (): the Print(_private_variable) - - if __name__=='__main__': - call_local () + Call_global () -Call_private ()
The above code first uses the VARs () function to show the original built-in variables in the module, mainly __doc__,__name__ and so on. The global variable global_variable and the private global variable are then defined _private_variable,call_local The local variable is called in the function, in the Call_global function, First, the global variable is declared by the keyword, then it is called, but the Call_private function does not have a global declaration of the private globals, but also successfully called, which involves the Python LEGB law.
{‘__doc__': None,‘__name__‘:‘__main__‘,‘__builtins__': <module‘Builtins' (built-in);‘__file__‘:‘C:\\users\\ekelike\\documents\\python note\\3_program_structure\\3.4_variable\\variable_type.py ", ' __package__ ": None, ' __loader__": <class _frozen_importlib. Builtinimporter ' _ _spec__ ' : none}this is< Span style= "COLOR: #000000" > local variablethis is global< Span style= "COLOR: #000000" > variablethis is private variable
Finally, we try to import the private global variable _private_variable of the current module in another module,
1 fromVariable_typeImport*2 3 Print(global_variable)4 5 Try:6 Print(_private_variable)7 exceptNameerror:8 Print("Name Error, re-import.")9 fromVariable_typeImport_private_variableTen Print(_private_variable)
As you can see from the output, you cannot import a private global variable by using the From module import *, but you can specify a specific variable name.
is Global variableName Error, re-import is private variable
Note: Global private variables here use a single underline and a double underline effect.
Related reading
1. LEGB Law
Python program structure [3]-Variable type/variable