Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How does this happen?

>Solution :

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")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading