Python - TypeError: argument of type 'function' is not iterable
<p>With help from people here and my own attempts I have the below (ITEM comes from a menu).  Encode/decode hex are working, and base64 + url encoding/decoding were working until I added the if statement. </p>

<p>I need the if statement though to check for hex as that uses a different layout to the other commands.  I've tried to use <code>s.encode("hex")</code> and <code>s.decode("hex")</code> as previously suggested with little progress.</p>

<p>Anyway when running the script, as I say hex works fine, if I choose base64 or url encode/decode though the following error is reported:</p>

<pre><code>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
</code></pre>

<p>My def is below.</p>

<pre><code>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))
</code></pre>

<p><em>Updated</em> - If i comment out the if statement, base64 + url work fine.  So I'm sorry I don't understand why it works like this but not with the IF which is checking to see if hex encode/decode is being performed.</p>

<pre><code>        #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))
</code></pre>

<p><em>further update</em> - Following Aesthete's help.  Now when running base64/url encode do work, however when running hex get a different error (added prints to show what the variables contain)</p>

<pre><code>        if process == encodinghex:
            print "process : " + process
            print "encodinghex contains : " + encodinghex           
            var = '"%s"' % (var)
            bottomtext.insert(INSERT, eval(var, process))
        elif process == decodinghex:
            var = '"%s"' % (var)
            bottomtext.insert(INSERT, eval(var, process))
        else:
            bottomtext.insert(INSERT, process(var))


process : .encode("hex")
encodinghex contains : .encode("hex")
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 33, in getText
    bottomtext.insert(INSERT, eval(var, process))
TypeError: globals must be a dict
</code></pre>

<p><strong>For anyone who ever has a similar issue - this is what fixed it for me, b64/url and hex working.</strong></p>

<pre><code>    if process == encodinghex:
        print "process : " + process
        print "encodinghex contains : " + encodinghex

        var = '"%s"' % (var)
        bottomtext.insert(INSERT, eval(var + process))
    elif process == decodinghex:
        var = '"%s"' % (var)
        bottomtext.insert(INSERT, eval(var + process))
    else:
        bottomtext.insert(INSERT, process(var))
</code></pre>
