Skip to content

Commit d0e9b05

Browse files
committed
less useless crap
1 parent c579abf commit d0e9b05

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

advanced/magicmethods.md

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ it's explained
8585
[here](https://docs.python.org/3/reference/datamodel.html#special-method-lookup)
8686
if you want to know more about it.
8787

88-
## str() and repr()
88+
## String representations
8989

9090
You have probably noticed that typing something to the interactive `>>>`
9191
prompt is not quite the same thing as printing it. For example,
@@ -99,46 +99,32 @@ hello
9999
>>>
100100
```
101101

102-
Typing `'hello'` without a print displayed it with quotes, but printing
103-
it displayed it without the quotes. Both of these can be customized with
104-
magic methods. `print('hello')` does the same thing as
105-
`print(str('hello'))`, and typing `'hello'` without a print does roughly
106-
the same thing as `print(repr('hello'))`.
102+
If you want to print something the way it's displayed on the `>>>`
103+
prompt you can use the `repr()` function. Here "repr" is short for
104+
"representation".
107105

108106
```python
109-
>>> repr('hello') # a string that literally contains 'hello'
110-
"'hello'"
111-
>>> str('hello') # same as just 'hello'
112-
'hello'
113-
>>> print(repr('hello'))
114-
'hello'
115-
>>> print(str('hello'))
116-
hello
107+
>>> message = 'hello'
108+
>>> print("the message is", repr(message))
109+
the message is 'hello'
117110
>>>
118111
```
119112

120-
The `repr()` values can also be easily used with string formatting.
121-
Instead of this...
122-
123-
```python
124-
print("message is " + repr(message))
125-
```
126-
127-
...you can use one of these instead:
113+
Combining `repr()` with [string
114+
formatting](../basics/handy-stuff-strings.md#string-formatting) is also
115+
easy. `%` formatting has a `%r` formatter, and `.format()` formatting
116+
has a `!r` flag.
128117

129118
```python
130-
print("message is %r" % (message,))
131-
print("message is {!r}".format(message))
119+
>>> print("the message is %r" % (message,))
120+
the message is 'hello'
121+
>>> print("the message is {!r}".format(message))
122+
the message is 'hello'
123+
>>>
132124
```
133125

134-
## Customizing str() and repr() values
135-
136-
As usual, `repr(thing)` does the same thing as `thing.__repr__()` and
137-
`str(thing)` does the same thing as `thing.__str__()`. Usually you don't
138-
need to and you shouldn't define `__str__` yourself because `__str__`
139-
defaults to `__repr__`.
140-
141-
Here's an example that hopefully clarifies things:
126+
The `__repr__` magic method can be used to customize this. For example,
127+
we can do this:
142128

143129
```python
144130
>>> class Website:
@@ -148,16 +134,12 @@ Here's an example that hopefully clarifies things:
148134
>>> w = Website()
149135
>>> w.__repr__()
150136
'<a Website object>'
151-
>>> repr(w)
152-
'<a Website object>'
153-
>>> w.__str__()
154-
'<a Website object>'
155137
>>> str(w)
156138
'<a Website object>'
157-
>>> w
158-
<a Website object>
159139
>>> print(w)
160140
<a Website object>
141+
>>> w
142+
<a Website object>
161143
>>>
162144
```
163145

0 commit comments

Comments
 (0)