Python-TypeError:类型为';功能';这是不可容忍的
我正在运行一些python代码,并得到一个错误:Python-TypeError:类型为';功能';这是不可容忍的,python,Python,我正在运行一些python代码,并得到一个错误: Exception in Tkinter callback Traceback (most recent call last): File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__ return self.func(*args) File "multiline14.py", line 28, in getText if encodinghe
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
File "multiline14.py", line 28, in getText
if encodinghex in process:
TypeError: argument of type 'function' is not iterable
我的def在下面
def gui(item):
def default_encode(s):
pass
# map items from menu to commands
encodinghex = '.encode("hex")'
decodinghex = '.decode("hex")'
mapping = {"encode_b64": base64.encodestring,"encode_url": urllib.quote_plus,"encode_hex": encodinghex, "decode_b64": base64.decodestring, "decode_url": urllib.unquote_plus, "decode_hex": decodinghex}
process = mapping.get(item, default_encode)
def getText():
#clear bottom text field
bottomtext.delete(1.0, END)
#var equals whats in middle
var = middletext.get(1.0, 'end-1c')
#insert encoded var in bottom
if encodinghex in process:
var = '"%s"' % (var)
bottomtext.insert(INSERT, eval(var + process))
elif decodinghex in process:
var = '"%s"' % (var)
bottomtext.insert(INSERT, eval(var + process))
else:
bottomtext.insert(INSERT, process(var))
是什么导致了这个错误?从示例的最后一行判断,您在某处有一个名为
process()
的函数。然而,如果在进程中编码十六进制,您尝试访问它,就好像它是行中的一个iterable一样。要修复错误,请更改函数或iterable的名称。您所做的似乎毫无意义。您有两个文本字符串,encodinghex和decodinghex,您正在使用eval
将其转换为要执行的代码。但是在映射
dict中,除了这些方法之外,还有各种实际的方法,您还试图将这些方法传递到eval
——这本身注定会失败,但即使在此之前,您的代码也试图将现有的文本字符串添加到实际的函数值中,这是不可能的。您正在从映射请求一个函数:
process = mapping.get(item, default_encode)
然后尝试在此处迭代:
if encodinghex in process:
除非主题是可编辑的,否则不能在
关键字中使用
在这里,您试图实现的是实际查看您对mapping.get()
的调用返回的函数
请注意,base64.encodestring,urllib.quote_plus,encodinghex,base64.decodestring,urllib.unquote_plus,decodinghex
都是函数令人惊讶的是,类型的参数“function”不可iterable
意味着你不能迭代函数如果我知道如何修复它,我显然不会问。有什么想法吗?你确定你知道eval()
的工作原理吗?这应该会有帮助:你们的问题已经偏离主题了。最初的问题是关于TypeError:type'function'的参数是不可编辑的
。接受这个问题的答案,如果必须的话,发布新的问题。但是我建议在提出进一步的问题之前研究和练习一些基本的python编程。您需要了解控制流、数据结构、函数等。试试这个:只有当process=encodinghex或decodinghex时才调用Eval,否则这是一个直接插入过程(var),我不太明白。process=mapping.get(项目,默认编码)已在前面指定。我试图检查从菜单中选择并输入到进程中的命令是否是用于编码或解码十六进制的命令,因为这两个命令的格式不同于url或base64。感谢您完成了q,但是如果我删除if语句,为什么base64/url编码/解码可以工作呢?i、 e.只留下bottomtext.insert(insert,process(var))?这是因为类型'function'不可调用,但类型function
当然是可调用的。看我的编辑。谢谢-这改变了一切。但是现在在实际的“bottomtext.insert(insert,eval(var,process))”上出现了一个新错误。在我看来(初学者-我的if语句现在不匹配?)-有什么想法吗?
if process == encodinghex: