3

Possible Duplicate:
Python: How to print a class or objects of class using print()?

I currently have this code:

class Track(object):
    def __init__(self,artist,title,album=None):
        self.artist = artist
        self.title = title
        self.album = album

    def __str__(self):
        return self.title + self.artist + self.album

Now when I put something like Track('Kanye West','Roses','Late Registration') into the terminal I get <__main__.Track object at 0x10f3e0c50> How can I get it to return or print the value at that place?

I'm new to programming and especially new to 'object oriented programming', so my question is what exactly is a class? How do I define a function within a class?

Community
  • 1
  • 1
iamtesla
  • 423
  • 2
  • 5
  • 18

3 Answers3

10

You should define __repr__ method:

class Track(object):

    ...

    def __repr__(self):
        return ('Track(artist=%s album=%s title=%s)' 
                % (repr(self.artist), repr(self.title), repr(self.album)))

Now you can see representation of object in terminal:

>>> Track('Kanye West','Roses','Late Registration')
Track(artist='Kanye West' album='Roses' title='Late Registration') 

Note that there is a difference between __str__ and __repr__ method:

  • __str__ needs to convert an object into a string. Calling: str(obj)
  • __repr__ - for its human readable visualization. Calling: repr(obj)

When you put into the terminal obj, python calls repr function automatically. But if you use print obj expression, python calls str function:

>>> obj
...repr of obj here...
>>> print obj
...str of obj here...

See doc for more information.

defuz
  • 26,721
  • 10
  • 38
  • 60
  • 2
    `__repr__` should create a valid Python expression to recreate the object. So you should either change your formatting or use `__str__` instead. See http://docs.python.org/reference/datamodel.html#object.__repr__ – Achim Oct 09 '12 at 14:17
  • 1
    although this yields the desired result, this is probably not what the OP wanted, and not within the range of the OPs obviously limited python skillset - i think he just wants to know how to print the answer, a simple solution (like mine) is to just "print" – Inbar Rose Oct 09 '12 at 14:23
3

Your code is fine, but you are forgetting something.

Lets say you have this class:

class Bob():
    def __init__(self, hobby):
        self.hobby = hobby

In the console you would make a Bob class with a hobby like this:

a = Bob("skiing")

then to get Bob's hobby you can do:

print a.hobby

now - back to your problem, you did Track('Kanye West','Roses','Late Registration')

you created an object, but you did not assign the object to a variable. so your result is the object itself... you could simply print the object, which will invoke the __str__ method. so...

print Track('Kanye West','Roses','Late Registration')

would work, but if you wanted to do it a bit nicer. (for example)

a = Track('Kanye West','Roses','Late Registration')
print a.title
print a.artist
print a.album
mgilson
  • 300,191
  • 65
  • 633
  • 696
Inbar Rose
  • 41,843
  • 24
  • 85
  • 131
0

If you need this just for debuging purpose to inspect internal values of your object you can do it like this

Track('Kanye West','Roses','Late Registration').__dict__

It will show you internal data representation as a python dict

yakxxx
  • 2,841
  • 2
  • 21
  • 22
  • Rather than explicitly accessing `__dict__`, I think it looks a little cleaner to use `vars(name_of_object)`. – DSM Oct 09 '12 at 14:23