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

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Dec 7 10:19:46 EST 2009


Victor Subervi wrote:
>
>
>              printTree(aTree[name], level + 1)
>
>
> ...     print aTree([name], level + 1)
> ...
> Traceback (most recent call last):
>   File "<stdin>", line 4, in ?
> TypeError: 'dict' object is not callable
> >>>
>
Be cautious, you are now executing the same code !
Again, read carefully the error message, you tried to call a dictionary 
: aTree([name], level + 1)

the following code below works fine in a python 2.5 shell:

allTrees = [{'prodCat1': {}, 'prodCat2': {}}, {'presCat1': {}, 
'presCat2': {}}]
level = 0
tree=[]
def printTree(allTrees, level=0):
    for aTree in allTrees:
        for name in sorted(aTree.keys()):
            print '\t' * level, name
            tree.append("%s%s" % ("\t" * level, name))
            printTree(aTree[name], level + 1)


In [9]: run ~/test.py

In [10]: printTree(allTrees)
 prodCat1
 prodCat2
 presCat1
 presCat2

In [11]: print tree
['prodCat1', 'prodCat2', 'presCat1', 'presCat2']

Though I'm not sure this is exactly want you want.
Anyway, there is a big probelm happening when  using the following value :
allTrees = [{'prodCat1': {'test':'success'}, 'prodCat2': {}}, 
{'presCat1': {}, 'presCat2': {}}]

printTree take a list in the first place, but when recursively calling 
printTree(aTree[name], level + 1), you pass a dictionary. Your code will 
fail as soon as the dictionary is not empty.

JM



More information about the Python-list mailing list