The rule of literal string

Discussion in 'Python' started by Li Han, Dec 17, 2008.

  1. Li Han

    Li Han Guest

    Hi! I just began to read the tutorial of python3.0 and I just can't
    figure out the rule of literal string. There is a example in the
    tuotrial:
    >>> '"Isn\'t," she said.'

    '"Isn\'t," she said.'
    It is not what I want, I just want '"Isn't," she said.' be printed,
    why the backslash failed?
    These don't work at all:
    >>> '''"Isn't," she said.'''

    '"Isn\'t," she said.'
    >>> r"Isn't," she said.

    SyntaxError: invalid syntax (<pyshell#6>, line 1)
    >>> r'"Isn't," she said.'

    SyntaxError: invalid syntax (<pyshell#7>, line 1)
    I have tried to solve it until my brain damaged and now I need to
    sleep.
    Li Han, Dec 17, 2008
    #1
    1. Advertising

  2. Li Han

    Li Han Guest

    Thank you, Chris! You have made me learn more about Python.
    Li Han, Dec 17, 2008
    #2
    1. Advertising

  3. Li Han

    Li Han Guest

    But what repr() do remain a black hole!
    Han
    Li Han, Dec 17, 2008
    #3
  4. Li Han

    Li Han Guest

    On 12ÔÂ18ÈÕ, ÉÏÎç7ʱ12·Ö, Scott David Daniels <> wrote:
    Scott wrote:
    > Try: print repr(repr("'"))
    > that might enlighten you.


    I found that print( repr( repr( arbitarystring ) ) ) == repr
    ( arbitarystring )
    Li Han, Dec 17, 2008
    #4
  5. Li Han

    Chris Rebert Guest

    2008/12/17 Li Han <>:
    > On 12月18æ—¥, 上åˆ7æ—¶12分, Scott David Daniels <> wrote:
    > Scott wrote:
    >> Try: print repr(repr("'"))
    >> that might enlighten you.

    >
    > I found that print( repr( repr( arbitarystring ) ) ) == repr
    > ( arbitarystring )


    As I stated previously, the key rule is:

    eval(repr(something)) == something

    That is, repr() gives a string of Python code that, when evaluated,
    results in what you gave to repr().

    So repr('') ==> "''"
    And repr("''") ==> "\"''\""
    Which when print()-ed is: "''"
    And eval("''") is the same as entering two apostrophes ('') at the
    REPL, both of which give an empty string object.

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    Chris Rebert, Dec 17, 2008
    #5
  6. Li Han

    James Mills Guest

    On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert <> wrote:
    > As I stated previously, the key rule is:
    >
    > eval(repr(something)) == something


    This rule is only true for basic data types;

    For example:

    >>> eval(repr(1)) == 1

    True
    >>> eval(repr([1, 2, 3])) == [1, 2, 3]

    True
    >>> eval(repr({"a": 1, "b": 2, "c": 3})) == {"a": 1, "b": 2, "c": 3}

    True
    >>> eval(repr("foo")) == "foo"

    True

    I guess the key thing here is that the repr
    implementation (__repr__) for str, int, float
    list and dict return sensible represenations
    that Python _can_ evaluate with eval(...)

    --JamesMills
    James Mills, Dec 17, 2008
    #6
  7. Li Han

    Chris Rebert Guest

    On Wed, Dec 17, 2008 at 3:34 PM, James Mills
    <> wrote:
    > On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert <> wrote:
    >> As I stated previously, the key rule is:
    >>
    >> eval(repr(something)) == something

    >
    > This rule is only true for basic data types;
    >
    > For example:
    >
    >>>> eval(repr(1)) == 1

    > True
    >>>> eval(repr([1, 2, 3])) == [1, 2, 3]

    > True
    >>>> eval(repr({"a": 1, "b": 2, "c": 3})) == {"a": 1, "b": 2, "c": 3}

    > True
    >>>> eval(repr("foo")) == "foo"

    > True
    >
    > I guess the key thing here is that the repr
    > implementation (__repr__) for str, int, float
    > list and dict return sensible represenations
    > that Python _can_ evaluate with eval(...)
    >
    > --JamesMills
    >


    True, I oversimplified to make things easier to understand. Strictly
    speaking, only the basic types make the guarantee I stated. Arbitrary
    types can have arbitrary, non-eval()-able repr()s. But the docs do
    state eval()-ability as a goal:

    repr(object)
    Return a string containing a printable representation of an
    object. [...] For many types, this function makes an attempt to return
    a string that would yield an object with the same value when passed to
    eval(), otherwise the representation is a string enclosed in angle
    brackets that contains the name of the type of the object together
    with additional information often including the name and address of
    the object. A class can control what this function returns for its
    instances by defining a __repr__() method.

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    Chris Rebert, Dec 17, 2008
    #7
  8. Li Han

    Li Han Guest

    Chris worte:
    [snip]
    > And repr("''") ==> "\"''\""
    > Which when print()-ed is: "''"
    > And eval("''") is the same as entering two apostrophes ('') at the
    > REPL, both of which give an empty string object.


    On my machine:
    >>> repr("''")

    '"\'\'"'
    Han
    Li Han, Dec 17, 2008
    #8
  9. Li Han

    Chris Rebert Guest

    On Wed, Dec 17, 2008 at 3:52 PM, Li Han <> wrote:
    > Chris worte:
    > [snip]
    >> And repr("''") ==> "\"''\""
    >> Which when print()-ed is: "''"
    >> And eval("''") is the same as entering two apostrophes ('') at the
    >> REPL, both of which give an empty string object.

    >
    > On my machine:
    >>>> repr("''")

    > '"\'\'"'
    > Han


    Well, I was kinda winging it and didn't actually use the interpreter
    for some of those. The point is that the output I gave _could_
    reasonably be that of the interpreter (if the implementation details
    of repr() were slightly different; it would still meet the spec
    anyway) and illustrates the eval()-repr() relationship.

    Just keep in mind the equivalence rule and what repr() does should
    become obvious.

    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
    Chris Rebert, Dec 18, 2008
    #9
  10. On Wed, 17 Dec 2008 15:25:41 -0800, Chris Rebert wrote:

    > 2008/12/17 Li Han <>:
    >> On 12月18æ—¥, 上åˆ7æ—¶12分, Scott David Daniels <>

    wrote:
    >> Scott wrote:
    >>> Try: print repr(repr("'"))
    >>> that might enlighten you.

    >>
    >> I found that print( repr( repr( arbitarystring ) ) ) == repr (
    >> arbitarystring )

    >
    > As I stated previously, the key rule is:
    >
    > eval(repr(something)) == something
    >
    > That is, repr() gives a string of Python code that, when evaluated,
    > results in what you gave to repr().


    That is not true in general.


    >>> from urllib2 import HTTPError
    >>> h = HTTPError("404 quoth the Raven", None, None, None, None)
    >>> eval(repr(h))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 1, in <module>
    TypeError: __init__() takes exactly 6 arguments (1 given)


    Even for built-ins, it's not always true:

    >>> eval(repr(float('inf')))

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "<string>", line 1, in <module>
    NameError: name 'inf' is not defined


    In other words, there is no guarantee that repr(obj) will round-trip
    correctly, or at all. It's a Nice To Have, and it will often work, but
    it's not something you should rely on for arbitrary objects.

    However, I believe it is true for strings.


    --
    Steven
    Steven D'Aprano, Dec 18, 2008
    #10
  11. On Thu, 18 Dec 2008 09:34:12 +1000, James Mills wrote:

    > On Thu, Dec 18, 2008 at 9:25 AM, Chris Rebert <> wrote:
    >> As I stated previously, the key rule is:
    >>
    >> eval(repr(something)) == something

    >
    > This rule is only true for basic data types;


    Oops, missed this, and the follow ups.

    Note to self: read thread *before* replying.


    --
    Steven
    Steven D'Aprano, Dec 18, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    626
    John C. Bollinger
    Aug 27, 2003
  2. Replies:
    5
    Views:
    720
    Frank Schmitt
    Sep 22, 2003
  3. herrcho
    Replies:
    7
    Views:
    412
    Jack Klein
    Sep 26, 2003
  4. Replies:
    0
    Views:
    1,349
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    364
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page