变量的作用率
变量作用范围限制
分类:按照作用域分类
全局(global):在函数外部定义
局部(local):在函数内部定义
变量的作用范围:
全局变量:在全局范围都是有效的
全局变量在局部可以使用(即函数内部可以访问函数外部定义的变量)
局部变量只可以在局部范围使用
局部变量在全局范围不可以使用
LEGB原则
L (Local)局部作用域
E (Enclosing Function locale)外部嵌套函数作用域
G (Global module)函数所在模块作用域
B(Buildin) Python内置魔抗的作用域
In [24]:
b1
1
def fun():
2
global b1
3
b1 = 100
4
print(b1)
5
print(“I am in fun”)
6
b2 = 99
7
print(b2)
8
9
fun()
10
print(b1)
100
I am in fun
99
100
globals, locals
可以通过globals和locals显示出局部变量和全局变量
参考一下案例
In [18]:
建函数
1
globals 和 locals
2
globals 和 locals 叫做内建函数
3
4
5
a = 1
6
b = 2
7
8
def fun(c, d):
9
e = 111
10
print(“locals={0}”.format(locals()))
11
print(“globals={0}”.format(globals()))
12
13
fun(100, 200)
locals={‘c’: 100, ‘d’: 200, ‘e’: 111}
globals={‘name’: ‘main’, ‘doc’: ‘Automatically created module for IPython interactive environment’, ‘package’: None, ‘loader’: None, ‘spec’: None, ‘builtin’: <module ‘builtins’ (built-in)>, ‘builtins’: <module ‘builtins’ (built-in)>, ‘_ih’: [’’, ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(1oo, 2oo)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’, ‘# globals 和 locals\n# globals 和 locals 叫做内建函数\n\n\na = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’], ‘oh’: {}, ‘dh’: [‘C:\Users\user’], ‘In’: [’’, ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(1oo, 2oo)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’, ‘# globals 和 locals\n# globals 和 locals 叫做内建函数\n\n\na = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’], ‘Out’: {}, ‘get_ipython’: <bound method InteractiveShell.get_ipython of <ipykernel.zmqshell.ZMQInteractiveShell object at 0x00000228738E1EF0>>, ‘exit’: <IPython.core.autocall.ZMQExitAutocall object at 0x000002287395EF60>, ‘quit’: <IPython.core.autocall.ZMQExitAutocall object at 0x000002287395EF60>, '’: ‘’, '’: ‘’, '’: ‘’, ‘_i’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’, ‘_ii’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_iii’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i1’: ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘fun’: <function fun at 0x0000022873AEAF28>, ‘_i2’: ‘def fun():\n b1 = 100\n print(b1)\n print(“I am in fun”)\n b2 = 99\n print(b2)\n \nprint(b1)\nfun()’, ‘_i3’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(1oo, 2oo)’, ‘_i4’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘a’: 1, ‘b’: 2, ‘_i5’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i6’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i7’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i8’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i9’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i10’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i11’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n c = 12\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i12’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i13’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“Locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i14’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(Locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i15’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i16’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“Globals={0}”.format(Globals()))\n \nfun(100, 200)’, ‘_i17’: ‘a = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’, ‘_i18’: ‘# globals 和 locals\n# globals 和 locals 叫做内建函数\n\n\na = 1\nb = 2\n\ndef fun(c, d):\n e = 111\n print(“locals={0}”.format(locals()))\n print(“globals={0}”.format(globals()))\n \nfun(100, 200)’}
eval()函数
把一个字符串当做表达式来执行, 返回表达式执行后的结果
语法:
eval(string_code, globals=None, locals=None)
In [20]:
x = 100
q = 200
执行x + q
z1 = x + q
z2 = eval(“x + q”)
print(z1)
print(z2)
1
x = 100
2
q = 200
3
执行x + q
4
z1 = x + q
5
z2 = eval(“x + q”)
6
7
print(z1)
8
print(z2)
300
300
exec
1
exec函数
2
- 和eval函数功能类似,但是,不返回结果
3 - 语法:
4
5
exec(string_code, globals=None, locals=None)
In [22]:
1
x = 100
2
q = 200
3
执行x + q
4
z1 = x + q
5
z2 = exec(“x + q”)
6
7
print(z1)
8
print(z2)
300
None