Python Forum
How to avoid "None"s in the output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to avoid "None"s in the output
#1
Hi all. I don't know from where "None" come. Of course I should like to get rid of them.
class FirstClass:
	def setdata(self, value):
		self.data=value		
	def display(self):
		print(self.data)
		
x=FirstClass()
y=FirstClass()
x.setdata("King Arthur")
y.setdata(3.14159)
print(x.display())     #King Arthur
print(y.display())     #3.14159
x.data="Age of King Arthur"
print(x.display())     #Age of King Arthur
x.anotherInteger=67
print(x.anotherInteger)   # 67
And now the Output
Output:
λ python class1.py King Arthur None 3.14159 None Age of King Arthur None 67
Reply
#2
You get None printed because FirstClass.display function does not return anything so it does return the default None.
In your code you print from within the function, so there is no need to print again when calling the function, e.g.
replace print(x.display()) with just x.display()
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Avoid output buffering when redirecting large data (40KB) to another process Ramphic 3 3,391 Mar-10-2018, 04:49 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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