python获取变量的变量-动态变量

前言全局说明


一、说明

1.1 环境:

Windows 11 家庭版 24H2 26100.4061
Python 3.8.10 (tags/v3.8.10:3d8993a, May  3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)] on win32

二、动态变量名处理(百度AI)

对于动态生成的变量名,可以通过globals()或locals()字典配合字符串操作来访问。这种方法适用于批量处理具有特定命名模式的变量:

var1 = "value1"
var2 = "value2"
for i in range(1, 3):
    print(globals()[f"var{i}"])  # 依次输出: value1, value2

globals()[] 是固定写法




免责声明:本号所涉及内容仅供安全研究与教学使用,如出现其他风险,后果自负。




参考、来源:
https://www.runoob.com/w3cnote/python-dynamic-var.html
https://chat.baidu.com/search?isShowHello=1&replace=1&extParams={"ori_lid"%3A"8870273417506356678"%2C"enter_type"%3A"sidebar_dialog"%2C"subEnterType"%3A"his_middle"%2C"forceRefresh"%3A1762412588569}
https://blog.51cto.com/u_16213368/12794863
https://docs.pingcode.com/ask/1038440.html
https://docs.pingcode.com/ask/1050686.html
https://blog.csdn.net/qq_27084325/article/details/97511397



posted @ 2025-11-06 15:17  悟透  阅读(4)  评论(0)    收藏  举报