【发布时间】:2011-04-28 18:37:41
【问题描述】:
arguments=dict()
if (arg.find("--help") == 0):
arguments["help"] = 1
if help in arguments:
#this doesnt work
print(arguments["help"]) # This will print 1
无法确定某个键是否已定义。 .has_key 在 2.7 中已被弃用,除此之外我还没有找到其他解决方案。我做错了什么?
【问题讨论】:
-
如果你在解析参数,为什么不直接使用
optparse? (docs.python.org/library/optparse.html) -
optparse 已被 argparse 取代,它将在 Python 的未来版本中维护(尽管目前我认为没有非常实质性的差异)。我相信它受到 2.7+ 和 3.2+ 的支持。
标签: python dictionary key-value