Python Forum
TypeError: __str__ returned non-string error - 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 error (/thread-15583.html)



TypeError: __str__ returned non-string error - jolinchewjb - Jan-23-2019

the result is : TypeError: __str__ returned non-string (type NoneType), but i think my __str__ function is correct,any idea? thanks, this is under python 3.5.1

class srvinfo(object):

def __init__(self,id,name=None,comp=None):
self.id=id
self.name=name

self.comp=comp

def change_usr(self,id,name,comp):
self.id=id
self.name=name
self.comp=comp

def __str__(self):

return f '{self.id} {self.name} : {self.comp}'
s=srvinfo(1021)
print(s)
s.change_usr(1022,'Jeffery','Microsoft')
print(s)


RE: TypeError: __str__ returned non-string error - jolinchewjb - Jan-23-2019

class srvinfo(object):

def __init__(self,id,name=None,comp=None):
self.id=id
self.name=name

self.comp=comp

def change_usr(self,id,name,comp):
self.id=id
self.name=name
self.comp=comp

def __str__(self):

return f '{self.id} {self.name} : {self.comp}' 
s=srvinfo(1021)
print(s)
s.change_usr(1022,'Jeffery','Microsoft')
print(s)



RE: TypeError: __str__ returned non-string error - buran - Jan-23-2019

Please, when post code, make sure indentation is preserved. As is at the moment it is non-sense.
Also, post full traceback in error tags.
Finally, based on trying to use f-strings, this code is intended for python version 3.6 or higher. It cannot run on 3.5.1.


RE: TypeError: __str__ returned non-string error - jolinchewjb - Jan-23-2019

my question is how to translate: return f '{self.id} {self.name} : {self.comp}' in Python 3.6.1 to Python 3.5.1, i have to use Python 3.5.1


RE: TypeError: __str__ returned non-string error - buran - Jan-23-2019

use format()
return '{} {} : {}'.format(self.id, self.name, self.comp)



RE: TypeError: __str__ returned non-string error - jolinchewjb - Jan-24-2019

thanks