[Tutor] Fwd: Re: Why is this printing None?

Alex Kleider akleider at sonic.net
Wed Apr 15 19:43:20 EDT 2020


Sorry, I hit reply rather than reply-all

-------- Original Message --------
Subject: Re: [Tutor] Why is this printing None?
Date: 2020-04-15 16:42
 From: Alex Kleider <akleider at sonic.net>
To: Cranky Frankie <cranky.frankie at gmail.com>

On 2020-04-15 06:24, Cranky Frankie wrote:
> I have this simple class in Python 3.8. When I run this in IDLE or at 
> the
> command prompt the word None gets printed after the employee name. Why?
> 
> class Employee:
> 
>     def __init__ (self, name):
>         self.name = name
> 
>     def displayEmployee(self):
>         print("Name : ", self.name)
> 
> if __name__ == '__main__':
>     emp1 = Employee('Zara')
>     print(emp1.displayEmployee())
> 
> 

the last statement of your code is asking that the result of the 
displayEmployee method be printed.
Any method/function without a specific return statement returns None by 
default and that's what's getting printed.
Change your last line by removing the word 'print' (and, optionally, 
remove a set of ().)


More information about the Tutor mailing list