如何测试潜在变量名列表中的哪些元素位于命名空间中?我应该把它们初始化为“无”吗?或者它们是更好的方法吗?你知道吗
# Potential list. Won't work, will give NameError if one of them is not in the namespace
all_vars = [rds.rclass, \
rds.rlevel, \
rds.seg \] # the list is actually much longer
#Could do:
all_vars =[]
try:
all_vars.append(rds.rclass)
except NameError:
pass
....
#or as initially said:
rds.rclass = rds.rlevel = ... = None
还有别的(更好的)办法吗?你知道吗
很可怕,但这应该管用:
相关问题 更多 >
编程相关推荐