'Method returns NoneType object
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?
Solution 1:[1]
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")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Muhammad huzaifa |
