yot*_*moo 2 python attributeerror
我正在使用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'
Run Code Online (Sandbox Code Playgroud)
这是我定义词典的地方:
binary = {"add":'+', "sub":'-', "and":'&', "or":'|'}
relational = {"eq":"JEQ" , "lt":"JLT", "gt":"JGT"}
unary = {"neg":'-'}
Run Code Online (Sandbox Code Playgroud)
这是引发异常的函数:
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()
Run Code Online (Sandbox Code Playgroud)
此外,我尝试改变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
Run Code Online (Sandbox Code Playgroud)
我也厌倦了使用python 3.2
有任何想法吗?谢谢
在Python 3中,dict.has_key()它已经消失了(它已经被弃用了很长一段时间).请x in my_dict改用.
不过,你的问题不同了.虽然您的定义显示unary应该是字典,但回溯显示它实际上是一个函数.所以你没有展示的代码中的某个地方unary被重新定义.
即使unary是字典,你也无法称呼它.
| 归档时间: |
|
| 查看次数: |
1666 次 |
| 最近记录: |