@@ -35,7 +35,7 @@ attributes by default. We'll learn more about this when we'll talk about
35
35
36
36
** TODO:** write a ` classes2.md ` .
37
37
38
- ## Custom len()
38
+ ## Custom length
39
39
40
40
Let's get started by defining an object that has a length:
41
41
@@ -85,7 +85,7 @@ it's explained
85
85
[ here] ( https://docs.python.org/3/reference/datamodel.html#special-method-lookup )
86
86
if you want to know more about it.
87
87
88
- ## Custom str() and repr()
88
+ ## str() and repr()
89
89
90
90
You have probably noticed that typing something to the interactive ` >>> `
91
91
prompt is not quite the same thing as printing it. For example,
@@ -117,6 +117,22 @@ hello
117
117
>> >
118
118
```
119
119
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
+
120
136
As usual, ` repr(thing) ` does the same thing as ` thing.__repr__() ` and
121
137
` str(thing) ` does the same thing as ` thing.__str__() ` . Usually you don't
122
138
need to and you shouldn't define ` __str__ ` yourself because ` __str__ `
@@ -145,26 +161,6 @@ Here's an example that hopefully clarifies things:
145
161
>> >
146
162
```
147
163
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
-
168
164
The ` __repr__ ` method can return any string, but usually you should
169
165
follow one of these styles:
170
166
@@ -219,34 +215,40 @@ follow one of these styles:
219
215
# # Other magic methods
220
216
221
217
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
223
219
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 .
227
223
228
224
# # When should we use magic methods?
229
225
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.
242
241
243
242
# # Summary
244
243
245
244
- Magic methods define what instances of a class can do and how, like
246
245
" 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.
248
249
- 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.
250
252
Other than that, magic methods are usually not worth it.
251
253
252
254
***
0 commit comments