The rule of literal string

L

Li Han

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.'
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:SyntaxError: invalid syntax (<pyshell#7>, line 1)
I have tried to solve it until my brain damaged and now I need to
sleep.
 
C

Chris Rebert

2008/12/17 Li Han said:
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
 
J

James Mills

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
 
C

Chris Rebert

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
 
L

Li Han

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.
'"\'\'"'
Han
 
C

Chris Rebert

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.
'"\'\'"'
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
 
S

Steven D'Aprano

2008/12/17 Li Han said:
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.

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:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top