python - Why FLAGS is storing a value like other variables
问题描述
In the following code I am getting the error that None type object has no attribute x. When I run the following code by removing FLAGS it works fine. But I want to assign these values to the FLAGS because in my actual code I want to assign different values to the FLAGS. How can I fix this problem?
FLAGS = None
c1 =[1.5, 2.0, 2.5]
for i in range(len(c1)):
FLAGS.x = c1[i]
print(FLAGS.x)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--x', type=float, default=2.0)
FLAGS = parser.parse_args()
解决方案
Your code above if __name__ == '__main__':
is being executed before the FLAGS variable has been initialized. Try something like this
import argparse
FLAGS = None
c1 =[1.5, 2.0, 2.5]
def main():
for i in range(len(c1)):
FLAGS.x = c1[i]
print(FLAGS.x)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--x', type=float, default=2.0)
FLAGS = parser.parse_args()
main()
推荐阅读
- c - 我无法在 CMD 和 powershell 中执行 C 程序
- soundcloud - 如何获取某些播客的所有播放次数?
- c# - 如何在 C# 中使用 ListBox 更改焦点
- plotly-python - Plotly Express:绘图中元素的顺序(python)
- vb.net - 如何在矩形数组中的三个不同按钮单击上添加三个不同的矩形
- excel - VBA - Excel 在打开时延迟和强制停止宏
- javascript - 尝试添加 onclick 功能
- 标记但单击句柄功能未定义
- python - 将分区 Parquet 文件读取到 Python 中的 DataFame(在内存中),其中列类型是数组的数组
- asp.net-core - 找不到 Asp.net 核心身份模型命名空间名称
- python - 用python中给定字符串中的其他字符替换指定字符