0
#!/usr/bin/env python

class SportsCar(object):

        def __init__(self,make,colour):
                self.make = make
                self.colour = colour
                self.horn = "BEEEEEEEEPPPPPPP"

        def honk(self):
                #now we can make some noise!
                print self.make,'  ',self.colour,'  ',self.horn
                print "Done "


mycar = SportsCar('Honda','silver')
#print mycar.make 
#print mycar.colour

print mycar.honk()


print "Good Bye!!"

The output of the above code is given below.

Honda    silver    BEEEEEEEEPPPPPPP
Done 
None
Good Bye!!

The first two lines of the output

Honda    silver    BEEEEEEEEPPPPPPP
Done

This is printed by mycar.honk().

I also understand the 4th line

Good Bye!!

I don't understand from where 'None' in the third line come from? Can someone please explain?

Also another related question

what is the difference between the declerations

class SportsCar:

and

class SportsCar(object):

I have been seeing both the declerations in different places.?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
liv2hak
  • 14,472
  • 53
  • 157
  • 270

2 Answers2

9

Functions always have a return value, None by default.

You are printing the return value of the .honk() method, which is the default None:

print mycar.honk()

You can just call mycar.honk() without the print statement. The method does it's own printing.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

You're not returning anything in your honk method, therefore it returns None by default.

Kyle Getrost
  • 707
  • 1
  • 5
  • 11