>>> a="1"
>>> b="a"
>>> print(a,b)
1 a
>>> print(a,locals()[b])
1 1
>>>
locals() 函数会以字典类型返回当前位置的全部局部变量。

>>> print(locals())

{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, 'a': 1, 'b': 'a'}

>>> print(locals()[b])  #当函数后面指定相应的变量则显示指定内存的变量
1
>>>

>>> print(locals()['__name__'])
__main__
>>> print(locals()['b'])
a
>>> print(locals()[b])
1
>>>

 



相关文章:

  • 2021-11-10
  • 2021-12-10
  • 2021-09-19
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-01
  • 2021-05-28
猜你喜欢
  • 2021-04-15
  • 2022-12-23
  • 2022-12-23
  • 2021-10-17
  • 2022-01-10
  • 2021-08-30
  • 2021-12-05
相关资源
相似解决方案