Skip to content

Commit 00dc3a6

Browse files
committed
working on magicmethods.md
1 parent db19273 commit 00dc3a6

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

advanced/magicmethods.md

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ attributes by default. We'll learn more about this when we'll talk about
3535

3636
**TODO:** write a `classes2.md`.
3737

38-
## Custom len()
38+
## Custom length
3939

4040
Let's get started by defining an object that has a length:
4141

@@ -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-
## Custom str() and repr()
88+
## str() and repr()
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,
@@ -117,6 +117,22 @@ hello
117117
>>>
118118
```
119119

120+
The `repr()` values can also be easily used with string formatting.
121+
Instead of this...
122+
123+
```py
124+
print("message is " + repr(message))
125+
```
126+
127+
...you can use one of these instead:
128+
129+
```py
130+
print("message is %r" % (message,))
131+
print("message is {!r}".format(message))
132+
```
133+
134+
## Customizing str() and repr() values
135+
120136
As usual, `repr(thing)` does the same thing as `thing.__repr__()` and
121137
`str(thing)` does the same thing as `thing.__str__()`. Usually you don't
122138
need to and you shouldn't define `__str__` yourself because `__str__`
@@ -145,26 +161,6 @@ Here's an example that hopefully clarifies things:
145161
>>>
146162
```
147163

148-
The `repr()` values can also be easily used with string formatting.
149-
Instead of this...
150-
151-
```py
152-
>>> message = 'hello'
153-
>>> print("message is " + repr(message))
154-
message is 'hello'
155-
>>>
156-
```
157-
158-
...you can do this:
159-
160-
```py
161-
>>> print("message is %r" % message)
162-
message is 'hello'
163-
>>> print("message is {!r}".format(message))
164-
message is 'hello'
165-
>>>
166-
```
167-
168164
The `__repr__` method can return any string, but usually you should
169165
follow one of these styles:
170166

@@ -219,34 +215,40 @@ follow one of these styles:
219215
## Other magic methods
220216

221217
There are many more magic methods, and I don't see any reason to list
222-
them all here. [The official
218+
them all here. [The official
223219
documentation](https://docs.python.org/3/reference/datamodel.html) has
224-
more information about magic methods if you need it. You can also just
225-
keep reading this tutorial, and we'll learn more about some of the most
226-
useful and commonly used magic methods.
220+
more information about magic methods if you need it. We'll go through
221+
using the most important magic methods in the rest of this tutorial, so
222+
if you just keep reading you'll learn more about them.
227223

228224
## When should we use magic methods?
229225

230-
Usually magic methods are overkill. `website.has_user(user)` or
231-
`user in website.userlist` is way better than something weird like
232-
`user @ website`. People expect `website.has_user(user)` to check if
233-
the user has registered on the website, but nobody knows what
234-
`user @ website` does. Explicit is better than implicit, and simple is
235-
better than complex.
236-
237-
On the other hand, adding informative `__repr__` methods can make a
238-
module much nicer to use. I recommend using `__repr__` methods in things
239-
that other people will import and use in their projects, but `__repr__`
240-
methods aren't worth it for simple scripts that aren't meant to be
241-
imported.
226+
There's nothing wrong with using `__init__` everywhere, but other than
227+
that, magic methods are usually not needed. `website.has_user(user)` and
228+
`user in website.userlist` are way better than something weird that we
229+
could do with magic methods like `user @ website`. People expect
230+
`website.has_user(user)` check if a user has registered on the website,
231+
but nobody can guess what `user @ website` does. Explicit is better than
232+
implicit, and simple is better than complex.
233+
234+
On the other hand, using magic methods when needed can turn something
235+
good into something great. Especially the `__repr__` method is useful
236+
because people can get a good idea of what an object is by just looking
237+
at it on the `>>>` prompt or printing it. I recommend using `__repr__`
238+
methods in things that other people will import and use in their
239+
projects, but `__repr__` methods aren't worth it for simple scripts that
240+
are not meant to be imported.
242241

243242
## Summary
244243

245244
- Magic methods define what instances of a class can do and how, like
246245
"does it have a length" or "what does it look like when I print it".
247-
- Python uses magic methods to implement many things internally.
246+
- Python uses magic methods to implement many things internally, and we
247+
can customize everything by implementing the magic methods
248+
ourselves.
248249
- Defining custom `__repr__` methods is often a good idea when making
249-
things that other people will import and use in their own projects.
250+
things that other people will import and use in their own projects,
251+
and the `__init__` method is very useful for many things.
250252
Other than that, magic methods are usually not worth it.
251253

252254
***

0 commit comments

Comments
 (0)