我正在使用 Python 2.7.2+,当试图查看字典是否包含给定的字符串值(名为 func)时,我得到了这个异常:
Traceback (most recent call last):
File "Translator.py", line 125, in <module>
elif type == Parser.C_ARITHMETIC : parseFunc()
File "Translator.py", line 95, in parseFunc
if unary.has_key(func) :
AttributeError: 'function' object has no attribute 'has_key'
这是我定义字典的地方:
binary = {"add":'+', "sub":'-', "and":'&', "or":'|'}
relational = {"eq":"JEQ" , "lt":"JLT", "gt":"JGT"}
unary = {"neg":'-'}
这是引发异常的函数:
def parseFunc():
func = parser.arg1
print func
output.write("//pop to var1" + endLine)
pop(var1)
if unary.has_key(func) : // LINE 95
unary()
return
output.write("//pop to var2" + endLine)
pop(var2)
result = "//"+func + endLine
result += "@" + var1 + endLine
result += "D=M" + endLine
result += "@" + var2 + endLine
output.write(result)
if binary.has_key(func) :
binary()
else : relational()
另外,我尝试更改if unary.has_key(func)
为,if func in unary
但后来我得到了
Traceback (most recent call last):
File "Translator.py", line 126, in <module>
elif type == Parser.C_ARITHMETIC : parseFunc()
File "Translator.py", line 95, in parseFunc
if func in unary:
TypeError: argument of type 'function' is not iterable
PS我也用python 3.2厌倦了它
有任何想法吗?谢谢