unsupported operand type(s) for %: 'NoneType' and 'tuple'

Victor Subervi victorsubervi at gmail.com
Mon Dec 7 09:14:00 EST 2009


On Mon, Dec 7, 2009 at 7:14 AM, Jean-Michel Pichavant <
jeanmichel at sequans.com> wrote:

> Victor Subervi wrote:
>
>>
>> global printTree = <function printTree>, allTrees = [{'prodCat1': {},
>> 'prodCat2': {}}, {'presCat1': {}, 'presCat2': {}}]
>>  /var/www/html/angrynates.com/cart/catTree.py <
>> http://angrynates.com/cart/catTree.py> in
>> printTree(allTrees=[{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {},
>> 'presCat2': {}}], level=0)
>>
>>   12     for name in sorted(aTree.keys()):
>>   13       print '\t' * level, name
>>   14       tree.append("%s%s") % ("\t" * level, name)
>>   15       printTree(aTree[name], level + 1)
>>   16
>> tree = ['%s%s'], tree.append = <built-in method append of list object>,
>> level = 0, name = 'prodCat1'
>>
>> TypeError: unsupported operand type(s) for %: 'NoneType' and 'tuple'
>>      args = ("unsupported operand type(s) for %: 'NoneType' and 'tuple'",)
>>
>> But according to the same error, level = 0 [the NoneType, I presume] and
>> name = 'prodCat1', which is most certainly not a tuple! Why the error?
>> TIA,
>> Victor
>>
>
> Come on Victor,
>
> Given the error, you can easily see that
>
> tree.append("%s%s") % ("\t" * level, name)
> is involved in the error you get.
>
> The correct thing may be
> tree.append("%s%s" % ("\t" * level, name))
>
> It should be easy for you to spot.
> FYI, tree.append('%s%s') returns None, then ("\t" * level, name) is the
> tuple applied to None through the % operator. That is why you get the above
> error.
>

You're absolutely right. This code was supplied to me by a lister. Had I
written it myself, I would have caught it. Reading his code, I didn't. I
will train myself from now on to write out the code when I come across such
errors in the future (and re-write my own as well) so that I can spot such
errors:

>>> level = 0
>>> name = 'one'
>>> tree = []
>>> tree.append("%s%s" % ("\t" * level, name))
>>>

However, the code threw one of those blasted HTTP 500 errors and the log had
this terse comment:

[Mon Dec 07 06:01:23 2009] [error] [client 208.84.198.58] Premature end of
script headers: catTree.py

Thank you. How helpful. Here's the code:

def printTree(allTrees, level=0):
  tree = []
  for aTree in allTrees:
    for name in sorted(aTree.keys()):
      tree.append("%s%s" % ("\t" * level, name))
      printTree(aTree[name], level + 1)

The problem must be occurring in the last line which creates a loop since
printTree is called only once at the very end of the entire script (where it
is returned to the referrer), but it seems perfectly logical to me. Please
advise.
V
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091207/dc7f1f76/attachment-0001.html>


More information about the Python-list mailing list