Python Classes

jskfan
jskfan used Ask the Experts™
on
Python Classes

in the example  below, when I run the program, it shows what it is supposed to be printed to the screen, but it also add the word "None"
not sure why, though I followed the same example from a tutorial, but I did not see the program displaying the word "None" in that tutorial.

Any idea?

Thank you

c
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
John TsioumprisSoftware & Systems Engineer

Commented:
Here is how you should have implement the class
class Point:
    def MoveT(self):
        print('Move')

    def DrawT(self):
        print('Draw')

point1 = Point()
point1.MoveT()
point1.DrawT()

Open in new window

The 'None' comes from the extra Print you have implemented...and because the method doesn't returns anything ...its 'None'
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
That's because both the functions don't return anything and this is why it returns None when you call the function within the Print statement i.e. Print(point1.move())

Author

Commented:
Print(point1.move())   Should print the word "Move", since it is calling the function Move

Print(point1.draw())   Should print the word "Draw", since it is calling the function Draw

Author

Commented:
I followed this youtube link below , you can forward it to 3 hours :26 minutes, the mentor did not get the None word when he ran the program..
https://www.youtube.com/watch?v=_uQrJ0TkZlc
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
Maybe you didn't pay attention to my post. As I said, since both the class methods don't Return anything and if you try to print them, they will return None other than executing the print statements defined in those Methods.

Try the following code and you will get an idea about how does it work...

class Point:
  def move(self):
    print('Move')
    return 'Move is returned by the move method'
  
  def draw(self):
    print('draw')
    return 'Draw is returned by the draw method'


point1 = Point()

print(point1.move())
print(point1.draw())

Open in new window

Author

Commented:
I see what you are saying, since the function does not return anything, it just runs the Print statement, so it returns None
I was just wondering why on the The video, there is no None returned.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
At 3:26:00 in the video, the author is talking about the find_max(numbers) function and that function Returns the max number.
Are you sure that you are taking that function as a reference?
John TsioumprisSoftware & Systems Engineer

Commented:
If you go to 3:26:25 (around) you will see that actually he returns
Clipboard01.jpg

Author

Commented:
sorry at 3:06:26
Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015
Commented:
He used point1.draw() not print(point1.draw()) like you. In other words, he is calling the class method but not printing it.

Author

Commented:
Thank you Guys.
Subodh Tiwari (Neeraj)Excel & VBA Expert
Most Valuable Expert 2018
Awarded 2015

Commented:
You're welcome!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial