-1
$\begingroup$

#Here's my code:

class Person:

def _init_(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

p1 = Person("Kate", 18)
print(p1.myfunc())

#Here's my outcome:

Hello my name is Kate 
None

Why is there 'None' after my sentence?

$\endgroup$

1 Answer 1

1
$\begingroup$

myfunc() does not have a return statement. In this case python defaults to returning None.

print( p1.myfunc() ) executes myfunc() which prints "Hello my name is Kate", then it prints the return value of myfunc(), which is None.

Since myfunc() already executes print() to display what you want, you only need to call it with p1.myfunc(), there is no need to print( p1.myfunc() ).

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.