0

I'm running some python code and get an error:

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

My def is below.

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))

What causes that error?

5
  • Surprisingly, argument of type 'function' is not iterable means you can't iterate a function Commented Jul 24, 2012 at 10:43
  • 1
    If i knew how to fix it, I obviously wouldn't be asking. Any ideas pls? Commented Jul 24, 2012 at 10:58
  • Are you sure you know how eval() works? This should help: docs.python.org/library/functions.html#eval Commented Jul 24, 2012 at 11:39
  • Your questions are now way off topic. The original question was regarding TypeError: argument of type 'function' is not iterable. Accept the answer that satisfies that question and post new questions if you must. Commented Jul 24, 2012 at 12:07
  • But I would recommend researching and practising some basic python programming before asking further questions. You need to learn about control flow, data structures, functions etc etc. Try this: learnpython.org Commented Jul 24, 2012 at 12:13

3 Answers 3

1

You are requesting a function from mapping here:

process = mapping.get(item, default_encode)

You then try and iterate it here:

if encodinghex in process:

You can't use the in keyword unless the subject is Iterable.

What you're trying to achieve here is to actually see which function your call to mapping.get() returned

if process == encodinghex:

Note that base64.encodestring, urllib.quote_plus, encodinghex, base64.decodestring, urllib.unquote_plus, decodinghex are all functions

Sign up to request clarification or add additional context in comments.

3 Comments

Forgive the q, but why then would base64/url encoding/decoding work if I remove the if statement? i.e. just leave bottomtext.insert(INSERT, process(var)) ?
It's because type 'function' is not iterable, but of course type function is callable. See my edits.
Thank you - that has changed things. Getting a new error though now on the actual 'bottomtext.insert(INSERT, eval(var, process))'. It looks to me (beginner - that my if statement isn't being matched now?) - Any ideas pls?
1

What you've done doesn't seem to make any sense at all. You have two text strings, encodinghex and decodinghex, which you're using eval to turn into code to execute. But in your mapping dict alongside those you've also got various actual methods, which you're also trying to pass to eval - which is bound to fail in itself, but even before that your code is trying to add the existing text string to the actual function value, which is impossible.

1 Comment

Eval is only called if process = encodinghex or decodinghex otherwise its a straight INSERT process(var)
0

Judging from the last line of your sample, you have a function called process() somewhere. Yet you try to access it as if it were an iterable in the line if encodinghex in process. To fix the error, change the name of either the function or the iterable.

1 Comment

Not sure I understand. process = mapping.get(item, default_encode) is specified earlier. I'm trying to be able to check if the command which has been selected from the menu, and then fed into process is one for encoding or decoding hex, as those 2 have a different format than url or base64. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.