【问题标题】:How do I check whether a variable is declared in Python?如何检查变量是否在 Python 中声明?
【发布时间】:2018-05-01 21:15:43
【问题描述】:

如何在不使用try、except 语句的情况下检查变量是否已声明?

【问题讨论】:

    标签: python python-3.x variables scope global-variables


    【解决方案1】:

    dir() 命令给出了全局命名空间中所有变量的列表。如果您的变量是x_val,请使用

    'x_val' in dir()
    

    【讨论】:

    • 太棒了!正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2014-03-14
    • 2015-08-25
    • 2012-01-01
    • 2015-09-11
    • 1970-01-01
    • 2021-01-31
    • 2022-10-28
    • 1970-01-01
    相关资源
    最近更新 更多