locals | 函数更新并以字典形式返回当前全部局部变量

locals | 函数更新并以字典形式返回当前全部局部变量

😏locals | 函数更新并以字典形式返回当前全部局部变量

  • 如果locals()出现在函数模块主体中,则会返回模块的默认局部变量
  • locals() 会返回直到函数本身位置的所有局部变量,local本行后面的变量不会被返回
  • 如果locals()只出现在模块中的某个函数定义中, 当声明此函数时,返回此函数中的局部变量与函数输入参数
# locals() 测试
# 在模块中 则返回所有全局局部变量
print(locals())
def locals_in_function():
    a = 10
    print(__file__)
    # 在函数中 则只返回参数跟局部变量
    print(locals())
    print(__file__ in locals())

locals_in_function()
😬运行结果:
{'__name__': '__main__', '__doc__': ' \ne:\\python\x0cile__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n', '__package__': None, '__loader__': None, '__spec__': None, '__builtin__': <module 'builtins' (built-in)>, '__builtins__': <module 'builtins' (built-in)>, '_ih': ['', 'from os import path\n\n\nprint(__file__)\n\nprint(locals())\n\nprint(path.dirname(__file__))\n\nprint("Pylance")\n\nprint(path.abspath(__file__))', 'from os import path\n\n# __file__ 测试\nprint(__file__)\nprint(path.dirname(__file__))\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(locals())\n\nlocals_in_function()', 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n\nlocals_in_function()', 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n    print(__file__ in locals())\n\nlocals_in_function()'], '_oh': {}, '_dh': [WindowsPath('e:/python'), WindowsPath('e:/python')], 'In': ['', 'from os import path\n\n\nprint(__file__)\n\nprint(locals())\n\nprint(path.dirname(__file__))\n\nprint("Pylance")\n\nprint(path.abspath(__file__))', 'from os import path\n\n# __file__ 测试\nprint(__file__)\nprint(path.dirname(__file__))\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(locals())\n\nlocals_in_function()', 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n\nlocals_in_function()', 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n    print(__file__ in locals())\n\nlocals_in_function()'], 'Out': {}, 'get_ipython': <function get_ipython at 0x000002BC64376670>, 'exit': <IPython.core.autocall.ZMQExitAutocall object at 0x000002BC662FFD90>, 'quit': <IPython.core.autocall.ZMQExitAutocall object at 0x000002BC662FFD90>, 'open': <function open at 0x000002BC64751040>, '_': '', '__': '', '___': '', '_VSCODE_types': <module 'types' from 'd:\\app\\code\\python\\python-3.9.5\\lib\\types.py'>, 'os': <module 'os' from 'd:\\app\\code\\python\\python-3.9.5\\lib\\os.py'>, '_VSCODE_hashlib': <module 'hashlib' from 'd:\\app\\code\\python\\python-3.9.5\\lib\\hashlib.py'>, '__VSCODE_wrapped_run_cell': False, '__VSCODE_compute_hash': <function __VSCODE_compute_hash at 0x000002BC66339AF0>, '__VSCODE_wrap_run_cell': <function __VSCODE_wrap_run_cell at 0x000002BC66339160>, 'sys': <module 'sys' (built-in)>, '__vsc_ipynb_file__': 'e:\\python\\file__.py', '__file__': 'e:\\python\\file__.py', '_i': 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n\nlocals_in_function()', '_ii': 'from os import path\n\n# __file__ 测试\nprint(__file__)\nprint(path.dirname(__file__))\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(locals())\n\nlocals_in_function()', '_iii': 'from os import path\n\n\nprint(__file__)\n\nprint(locals())\n\nprint(path.dirname(__file__))\n\nprint("Pylance")\n\nprint(path.abspath(__file__))', '_i1': 'from os import path\n\n\nprint(__file__)\n\nprint(locals())\n\nprint(path.dirname(__file__))\n\nprint("Pylance")\n\nprint(path.abspath(__file__))', 'path': <module 'ntpath' from 'd:\\app\\code\\python\\python-3.9.5\\lib\\ntpath.py'>, '_i2': 'from os import path\n\n# __file__ 测试\nprint(__file__)\nprint(path.dirname(__file__))\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(locals())\n\nlocals_in_function()', 'locals_in_function': <function locals_in_function at 0x000002BC6638F3A0>, '_i3': 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n\nlocals_in_function()', '_i4': 'from os import path\n\n# __file__ 测试\n""" \ne:\\python\\file__.py\ne:\\python \n\nprint(__file__)\nprint(path.dirname(__file__))\n"""\n\n# locals() 测试\nprint(locals())\ndef locals_in_function():\n    a = 10\n    print(__file__)\n    print(locals())\n    print(__file__ in locals())\n\nlocals_in_function()'}
e:\python\file__.py
{'a': 10}
False
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值