1
votes

Here is the code:

class className:
    def createName(self, name):
        self.name = name

    def displayName(self):
        return self.name

    def saying(self):
        print "Hello %s " % self.name

first = className()
second = className()

first.createName('Bob')
second.createName('Tony')


print first.displayName()
print second.displayName()

print 20*"_"

print first.saying()
print second.saying()

The question is the following - when I call the second method (saying) why is the result:

Hello Bob
None
Hello Tony
None

Why the None?

2

2 Answers

2
votes

All functions/methods that don't specify a return value will return None by default so you are just seeing the return value when you print the method.

Either return "Hello %s " % self.name instead of printing or just call the method without using print.

On a side note you don't need and should not use getters in python, to access self.name just print first.name etc..

0
votes

Return the value, don't print it:

def saying(self):
    return "Hello %s " % self.name