强调:eval()函数功能虽然强大,但是也很危险,这个方法需要慎重使用. 利用python中的内置函数 eval() ,函数说明: def eval(*args, **kwargs): # real signature unknown """ Evaluate the given source in the context of globals and locals. The source may be a string representing a Python expre…
在python中,函数会创建一个自己的作用域,也称为为命名空间.这意味着在函数内部访问某个变量时,函数会优先在自己的命名空间中寻找. 通过内置函数globals()返回的是python解释器能知道的变量名称的字典(变量名:值): 而locals()函数返回的是函数内部本地作用域中的变量名称字典.由此可以看出,函数都是由自己独立的命名空间的. 查看全局变量和局部变量: #coding=utf-8 outerVar="this is a global variable"def test()…