Python Forum
TypeError: __str__ returned non-string (type tuple) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: TypeError: __str__ returned non-string (type tuple) (/thread-33277.html)



TypeError: __str__ returned non-string (type tuple) - Anldra12 - Apr-12-2021

TypeError: __str__ returned non-string (type tuple)
The code are follow
from BTreeWordFinder import *
from BalancedBTree import *
class BalancedBTreeWordFinder(BTreeWordFinder):
'' 'binary search class implemented with a balanced binary tree; inherits from BTreeWordFinder and thus takes advantage of its methods' ''
def __init__(self, file, btree=None):
'' 'Builder methods' ''
BTreeWordFinder.__init__(self, file, btree)
@staticmethod
def main():
'' 'main method version of the class for the search engine implemented with a balanced tree' ''
b = BalancedBTreeWordFinder(BalancedBTreeWordFinder.askFile(), BalancedBTree())
b.findOcurrencesInFile('dictionary.txt')
b.viewIndex()
b.showDepth()
if __name__ == "__main__":
BalancedBTreeWordFinder.main()
The error line in class are follow
def __str__(self):
return str(self._val) # here is the error
Complete exestuation of return error are follow
Error:
Traceback (most recent call last): File "D:/btree-word-finder-master/btree-word-finder-master/Code_and_texts/BalancedBTreeWordFinder.py", line 20, in <module> BalancedBTreeWordFinder.main() File "D:/btree-word-finder-master/btree-word-finder-master/Code_and_texts/BalancedBTreeWordFinder.py", line 16, in main b.viewIndex() File "D:\btree-word-finder-master\btree-word-finder-master\Code_and_texts\BTreeWordFinder.py", line 48, in viewIndex self.getBTree().doinOrden() File "D:\btree-word-finder-master\btree-word-finder-master\Code_and_texts\BTree.py", line 80, in doinOrden self.inOrden(self.getRoot()) File "D:\btree-word-finder-master\btree-word-finder-master\Code_and_texts\BTree.py", line 89, in inOrden self.visit(probe) # visit mean to search on traversed left File "D:\btree-word-finder-master\btree-word-finder-master\Code_and_texts\BTree.py", line 107, in visit print(node) File "D:\btree-word-finder-master\btree-word-finder-master\Code_and_texts\Node.py", line 39, in __str__ return '%s %s %s'%(self._val) TypeError: __str__ returned non-string (type tuple)

Hope some expert help why return this error where modified by diffrent ways to correct but still i got the same error
The error here in below codes return for me where trying like different return
str(sel._val)  '%s %s %s'%(self._val)
, return '{} {} {}'.format(self._val) and template = '{self._val}'
    return template.format(self)
def __str__(self):
return str(self._val) # here is the error



RE: TypeError: __str__ returned non-string (type tuple) - Anldra12 - Apr-13-2021

Thanks @perfringo, @BashBedlam and the problem is solved based on post https://python-forum.io/Thread-AttributeError-tuple-object-has-no-attribute-format