Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
b4f12424
Commit
b4f12424
authored
May 07, 2003
by
Skip Montanaro
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace most uses of `...` by repr(...), noting that `...` is discouraged,
but convenient in interactive sessions.
parent
e9709e7e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
17 deletions
+15
-17
Doc/tut/tut.tex
Doc/tut/tut.tex
+15
-17
No files found.
Doc/tut/tut.tex
View file @
b4f12424
...
@@ -2765,9 +2765,9 @@ resulting from this formatting operation.
...
@@ -2765,9 +2765,9 @@ resulting from this formatting operation.
One question remains, of course: how do you convert values to strings?
One question remains, of course: how do you convert values to strings?
Luckily, Python has ways to convert any value to a string: pass it to
Luckily, Python has ways to convert any value to a string: pass it to
the
\function
{
repr()
}
or
\function
{
str()
}
functions
, or just write
the
\function
{
repr()
}
or
\function
{
str()
}
functions
. Reverse quotes
the value between reverse quotes (
\code
{
``
}
, equivalent to
(
\code
{
``
}
) are equivalent to
\function
{
repr()
}
, but their use is
\function
{
repr()
}
)
.
discouraged
.
The
\function
{
str()
}
function is meant to return representations of
The
\function
{
str()
}
function is meant to return representations of
values which are fairly human-readable, while
\function
{
repr()
}
is
values which are fairly human-readable, while
\function
{
repr()
}
is
...
@@ -2786,28 +2786,26 @@ Some examples:
...
@@ -2786,28 +2786,26 @@ Some examples:
>>> s = 'Hello, world.'
>>> s = 'Hello, world.'
>>> str(s)
>>> str(s)
'Hello, world.'
'Hello, world.'
>>>
`s`
>>>
repr(s)
"'Hello, world.'"
"'Hello, world.'"
>>> str(0.1)
>>> str(0.1)
'0.1'
'0.1'
>>>
`0.1`
>>>
repr(0.1)
'0.10000000000000001'
'0.10000000000000001'
>>> x = 10 * 3.25
>>> x = 10 * 3.25
>>> y = 200 * 200
>>> y = 200 * 200
>>> s = 'The value of x is ' +
`x` + ', and y is ' + `y`
+ '...'
>>> s = 'The value of x is ' +
repr(x) + ', and y is ' + repr(y)
+ '...'
>>> print s
>>> print s
The value of x is 32.5, and y is 40000...
The value of x is 32.5, and y is 40000...
>>> # Reverse quotes work on other types besides numbers:
>>> # The repr() of a string adds string quotes and backslashes:
... p = [x, y]
>>> ps = repr(p)
>>> ps
'[32.5, 40000]'
>>> # Converting a string adds string quotes and backslashes:
... hello = 'hello, world
\n
'
... hello = 'hello, world
\n
'
>>> hellos =
`hello`
>>> hellos =
repr(hello)
>>> print hellos
>>> print hellos
'hello, world
\n
'
'hello, world
\n
'
>>> # The argument of reverse quotes may be a tuple:
>>> # The argument to repr() may be any Python object:
... repr(x, y, ('spam', 'eggs'))
"(32.5, 40000, ('spam', 'eggs'))"
>>> # reverse quotes are convenient in interactive sessions:
... `x, y, ('spam', 'eggs')`
... `x, y, ('spam', 'eggs')`
"(32.5, 40000, ('spam', 'eggs'))"
"(32.5, 40000, ('spam', 'eggs'))"
\end{verbatim}
\end{verbatim}
...
@@ -2817,9 +2815,9 @@ Here are two ways to write a table of squares and cubes:
...
@@ -2817,9 +2815,9 @@ Here are two ways to write a table of squares and cubes:
\begin{verbatim}
\begin{verbatim}
>>> import string
>>> import string
>>> for x in range(1, 11):
>>> for x in range(1, 11):
... print string.rjust(
`x`, 2), string.rjust(`x*x`
, 3),
... print string.rjust(
repr(x), 2), string.rjust(repr(x*x)
, 3),
... # Note trailing comma on previous line
... # Note trailing comma on previous line
... print string.rjust(
`x*x*x`
, 4)
... print string.rjust(
repr(x*x*x)
, 4)
...
...
1 1 1
1 1 1
2 4 8
2 4 8
...
@@ -3357,7 +3355,7 @@ example:
...
@@ -3357,7 +3355,7 @@ example:
... def
__
init
__
(self, value):
... def
__
init
__
(self, value):
... self.value = value
... self.value = value
... def
__
str
__
(self):
... def
__
str
__
(self):
... return
`self.value`
... return
repr(self.value)
...
...
>>> try:
>>> try:
... raise MyError(2*2)
... raise MyError(2*2)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment