Python Learning note 6-function variables
Local variables and global variables
Any variable in Python has a specific scope
Variables defined in a function can be used only within the function, which can only be used in specific parts of the program we call local variables
Variables defined at the top of a file can be called by any function in the file, which can be used as a variable for the entire program we call global variables
[[email protected] ~]# vim value.py#!/usr/bin/pythonx=100def Fun (): Global x x +=1 Global y Y=1fun () print Xpri NT Y
[email protected] ~]# python value.py
101
1
If you do not use Global X and do not declare the local variable as a global variable, you will get an error
#global x
[email protected] ~]# python value.py
Traceback (most recent):
File "value.py", line 8, <module>
Fun ()
File "value.py", line 5, in fun
X +=1
unboundlocalerror:local variable ' x ' referenced before assignment
This article comes from "Plum blossom fragrance from bitter cold!" "Blog, be sure to keep this provenance http://daixuan.blog.51cto.com/5426657/1790094
Python Learning note 6-function variables