FAQ
Hey everyone,
This is a super noob question, so please be gentle.
I am working my way through "Learn Python the Hard Way" using both
python 2.7 and python 3.1 (I want to get a handle on the differences
between the two - the intention to write things in python 3 but be
able to understand things from python 2).

The first quarter or so of the book details lots of stuff about
strings. Most of the learning is by doing, with less of an emphasis on
the theory behind what certain things actually do. The issue is, I
can't seem to find some of the items in the documentation. Right now
what is stumping me... what exactly does %r do? I can't find it in the
documentation anywhere.
-Matty

Search Discussions

  • Chris Angelico at May 25, 2011 at 1:09 pm

    On Wed, May 25, 2011 at 11:06 PM, Matty Sarro wrote:
    Right now what is stumping me... what exactly does %r do?
    You're talking about the formatting operator? It's like the repr function:

    http://docs.python.org/library/functions.html#repr

    Chris Angelico
  • Chris Guidry at May 25, 2011 at 1:13 pm

    On Wednesday, May 25, 2011 9:06:02 AM UTC-4, Matty Sarro wrote:
    can't seem to find some of the items in the documentation. Right now
    what is stumping me... what exactly does %r do? I can't find it in the
    documentation anywhere.
    Matty, %r in a format string is very much like %s. %s calls str(your_object) in order to produce the resulting string. %r calls repr(your_object). Generally, you'd want to use %s for strings that will surface to a user, while %r is great for debugging and logging.

    Also, if you haven't come across repr(), you should be able to find that in the docs; it's also considered good practice to overload repr() in your own classes in order to provide a useful representation for your objects.
  • Matty Sarro at May 25, 2011 at 1:21 pm
    Thanks guys! I appreciate it. I was wondering why %r was always
    showing things enclosed in single-quotes.
    On Wed, May 25, 2011 at 9:13 AM, Chris Guidry wrote:
    On Wednesday, May 25, 2011 9:06:02 AM UTC-4, Matty Sarro wrote:
    can't seem to find some of the items in the documentation. Right now
    what is stumping me... what exactly does %r do? I can't find it in the
    documentation anywhere.
    Matty, %r in a format string is very much like %s. ?%s calls str(your_object) in order to produce the resulting string. ?%r calls repr(your_object). ?Generally, you'd want to use %s for strings that will surface to a user, while %r is great for debugging and logging.

    Also, if you haven't come across repr(), you should be able to find that in the docs; it's also considered good practice to overload repr() in your own classes in order to provide a useful representation for your objects.
    --
    http://mail.python.org/mailman/listinfo/python-list

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 25, '11 at 1:06p
activeMay 25, '11 at 1:21p
posts4
users3
websitepython.org

People

Translate

site design / logo © 2018 Grokbase