Python Forum
TypeError: __str__ returned non-string error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: __str__ returned non-string error
#1
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)
Reply
#2
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)
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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
Reply
#5
use format()
return '{} {} : {}'.format(self.id, self.name, self.comp)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error in class: TypeError: 'str' object is not callable akbarza 2 496 Dec-30-2023, 04:35 PM
Last Post: deanhystad
  How to access values returned from inquirer cspower 6 780 Dec-26-2023, 09:34 PM
Last Post: cspower
  boto3 - Error - TypeError: string indices must be integers kpatil 7 1,225 Jun-09-2023, 06:56 PM
Last Post: kpatil
  "TypeError: string indices must be integers, not 'str'" while not using any indices bul1t 2 2,001 Feb-11-2023, 07:03 PM
Last Post: deanhystad
  Error TypeError: output_type_handler() takes 2 positional arguments but 6 were given paulo79 1 1,926 Oct-17-2022, 06:29 PM
Last Post: paulo79
  TypeError: string indices must be integers JonWayn 12 3,374 Aug-31-2022, 03:29 PM
Last Post: deanhystad
  TypeError: float() argument must be a string or a number, not 'list' Anldra12 2 4,846 Jul-01-2022, 01:23 PM
Last Post: deanhystad
  "<class 'typeerror'>: don't know how to convert" error GiggsB 3 3,324 Feb-28-2022, 06:45 PM
Last Post: GiggsB
  SQLAlchemy Object Missing when Null is returned Personne 1 1,726 Feb-19-2022, 02:50 AM
Last Post: Larz60+
  TypeError: not enough arguments for format string MaartenRo 6 2,914 Jan-09-2022, 06:46 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020