Python| locals()函数
Python locals()函数返回当前本地符号表的字典。
- 符号表:它是由编译器创建的数据结构,用于存储执行程序所需的所有信息。
- 本地符号表:此符号表存储程序本地范围所需的所有信息,并且可以使用Python内置函数locals() 访问此信息。
Syntax : locals()
Parameters: This function does not takes any input parameter.
Return Type : This returns the information stored in local symbol table.
Python locals() 方法示例
示例 1: Python locals() 在本地范围内工作
Python3
# Python program to understand about locals
# here no local variable is present
def demo1():
print("Here no local variable is present : ", locals())
# here local variables are present
def demo2():
name = "Ankit"
print("Here local variables are present : ", locals())
# driver code
demo1()
demo2()
Python3
# Python program to understand about locals
# here no local variable is present
def demo1():
print("Here no local variable is present : ", locals())
# here local variables are present
def demo2():
name = "Ankit"
print("Here local variables are present : ", locals())
print("Before updating name is : ", name)
# trying to change name value
locals()['name'] = "Sri Ram"
print("after updating name is : ", name)
# driver code
demo1()
demo2()
Python3
# Python program to understand about locals
# data using locals
print("This is using locals() : ", locals())
# data using globals
print("This is using globals() : ", globals())
Python3
# Python3 program to demonstrate global() function
# global variable
a = 5
def func():
c = 10
d = c + a
# Calling globals()
globals()['a'] = d
print (d)
# Driver Code
func()
Python3
locals()
输出;
Here no local variable is present : {}
Here local variables are present : {'name': 'Ankit'}
示例 2:使用 locals() 更新
与 globals() 不同,此函数不能修改本地符号表的数据。下面的程序清楚地解释了它。
Python3
# Python program to understand about locals
# here no local variable is present
def demo1():
print("Here no local variable is present : ", locals())
# here local variables are present
def demo2():
name = "Ankit"
print("Here local variables are present : ", locals())
print("Before updating name is : ", name)
# trying to change name value
locals()['name'] = "Sri Ram"
print("after updating name is : ", name)
# driver code
demo1()
demo2()
输出:
Here no local variable is present : {}
Here local variables are present : {'name': 'Ankit'}
Before updating name is : Ankit
after updating name is : Ankit
示例 3:全局环境的 locals()
在全局环境的情况下,局部符号表与全局符号表相同。
Python3
# Python program to understand about locals
# data using locals
print("This is using locals() : ", locals())
# data using globals
print("This is using globals() : ", globals())
输出:
This is using locals() : {‘__name__’: ‘__main__’, ‘__doc__’: ‘Automatically created module for IPython interactive environment’, ‘__package__’: None, ‘__loader__’: None, ‘__spec__’: None, ‘__builtin__’:
Python本地 VS 全局函数
Python中的Python globals()函数返回当前全局符号表的字典。
Syntax: globals()
Parameters: No parameters required.
Python3
# Python3 program to demonstrate global() function
# global variable
a = 5
def func():
c = 10
d = c + a
# Calling globals()
globals()['a'] = d
print (d)
# Driver Code
func()
输出:
15
Python中的Python locals()函数返回当前本地符号表的字典。
Python3
locals()
输出:
{‘__name__’: ‘__main__’,
‘__doc__’: ‘Automatically created module for IPython interactive environment’,
‘__package__’: None,
‘__loader__’: None,
‘__spec__’: None,
‘__builtin__’:
‘__builtins__’:
‘_ih’: [”,
‘# Python program to demonstrate the use of\n# len() method \n\n# Length of below string is 5\nstring = “geeks” \nprint(len(string))\n\n# Length of below string is 15\nstring = “geeks for geeks” \nprint(len(string))’,
‘# Python program to demonstrate the use of\n# len() method \n\n# L