Classes and Objects in Python
I'm creating a bot in discord.py, and I have a class which stores all the information related to the user, like name, settings, preferences etc. When I try to get the name of the user by calling the user.get_name()
method(defined below), the method prints out the name and then returns a NoneType object as well! Why?
class User:
def __init__(self, name):
self.name = name
def get_name(self):
print(self.name)
user = User("Mike")
When run, the program prints:
Mike
None
How does this happen?
CodePudding user response:
Use return
in get_name
method,
class User:
def __init__(self, name):
self.name = name
def get_name(self):
print(self.name)
return self.name
user = User("Mike")