Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8389389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:42:50+00:00 2026-06-09T18:42:50+00:00

I’m learning about Classes and am having a problem with the return statement (is

  • 0

I’m learning about Classes and am having a problem with the return statement (is it a statement? I hope so), the program prints out nothing, it just ends without doing anything.

class className:
    def createName(self, name):
        self.name = name
    def displayName(self):
        return self.name
    def saying(self):
        print("Hello %s" % self.name)

first = className()
second = className()

first.createName("Jack")
second.createName("Joy")

first.displayName()
second.displayName()

I know I’m doing something so obviously wrong but I really don’t know what. I’d appreciate some help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:42:51+00:00Added an answer on June 9, 2026 at 6:42 pm

    To answer your question – return does not print anything, but it is slightly confusing, since the interactive python prompt does print out the value of the last statement e.g.:

    Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
    [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> 1+1
    2
    

    But if you create a file with contents 1+1 and run it as a python script, nothing is printed.

    Since you say that you are a newbie, I’ll give you a few pointers on how to improve your code.

    class className:
        def createName(self, name):
            self.name = name
        def displayName(self):
            return self.name
        def saying(self):
            print("Hello %s" % self.name)
    

    className has redundancy, you should rename your class just Name – also new style classes should always inherit object, so let’s change your definition a bit:

    class Name(object):
        def createName(self, name):
            self.name = name
        def displayName(self):
            return self.name
        def saying(self):
            print("Hello %s" % self.name)
    

    Creating something is done automatically by overriding the classes __init__() method. e.g:

    class Name(object):
        def __init__(self, name):
            self.name = name
        def displayName(self):
            return self.name
        def saying(self):
            print("Hello %s" % self.name)
    

    this way you can already initialize your name when instantiating your class, e.g.

    first = Name("Jack")
    

    Second, display is handled idiomatically by overriding the method __repr__ e.g.

    class Name(object):
        def __init__(self, name):
            self.name = name
        def __repr__(self):
           return self.name
        def saying(self):
            print("Hello %s" % self.name)
    

    This way, you only need to do two things:

    >>> n = Name("Jack")
    >>> print n
    Jack
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I know there's a lot of other questions out there that deal with this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.