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?

3
  • try str(Track('Kanye West','Roses','Late Registration')) Commented Oct 9, 2012 at 14:14
  • 3
    search first: stackoverflow.com/questions/1535327/… Commented Oct 9, 2012 at 14:14
  • I tried searching but I'm still a little confused, I guess I'm looking for an explanation in more everyday words. Commented Oct 9, 2012 at 14:17

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

__repr__ should create a valid Python expression to recreate the object. So you should either change your formatting or use __str__ instead. See docs.python.org/reference/datamodel.html#object.__repr__
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"
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

Comments

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

1 Comment

Rather than explicitly accessing __dict__, I think it looks a little cleaner to use vars(name_of_object).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.