2

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.

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Hyzenthlay
  • 463
  • 1
  • 7
  • 12
  • Looking briefly at the code it looks like you are calling first.displayName() which simply returns the name. If you want to print something you would either need to call: first.saying() or print("Hello %s" % first.displayName()) – Roger Aug 09 '12 at 03:45
  • I see, but isn't return supposed to return whatever is in self.name? – Hyzenthlay Aug 09 '12 at 04:14
  • yes it is, but you aren't printing it out anywhere right? You are just returning it. – Roger Aug 09 '12 at 04:18

1 Answers1

7

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
Community
  • 1
  • 1
Kimvais
  • 38,306
  • 16
  • 108
  • 142