You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/why.md
+6-26Lines changed: 6 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -250,39 +250,19 @@ is roughly
250
250
... else:
251
251
... return not result
252
252
...
253
-
... def __lt__(self, other):
254
-
... if other.__class__ is self.__class__:
255
-
... return (self.a, self.b) < (other.a, other.b)
256
-
... else:
257
-
... return NotImplemented
258
-
...
259
-
... def __le__(self, other):
260
-
... if other.__class__ is self.__class__:
261
-
... return (self.a, self.b) <= (other.a, other.b)
262
-
... else:
263
-
... return NotImplemented
264
-
...
265
-
... def __gt__(self, other):
266
-
... if other.__class__ is self.__class__:
267
-
... return (self.a, self.b) > (other.a, other.b)
268
-
... else:
269
-
... return NotImplemented
270
-
...
271
-
... def __ge__(self, other):
272
-
... if other.__class__ is self.__class__:
273
-
... return (self.a, self.b) >= (other.a, other.b)
274
-
... else:
275
-
... return NotImplemented
276
-
...
277
253
... def __hash__(self):
278
254
... return hash((self.__class__, self.a, self.b))
279
255
>>> ArtisanalClass(a=1, b=2)
280
256
ArtisanalClass(a=1, b=2)
281
257
```
282
258
283
259
That's quite a mouthful and it doesn't even use any of *attrs*'s more advanced features like validators or default values.
284
-
Also: no tests whatsoever.
260
+
If you pass `order=True`, there's even four more methods: `__lt__`, `__le__`, `__gt__`, and `__ge__`.
285
261
And who will guarantee you, that you don't accidentally flip the `<` in your tenth implementation of `__gt__`?
262
+
*attrs* will also do the right thing around [hashing](hashing):
263
+
It will write a `__hash__` for you if your class should be hashable and marks your class as unhashable if not.
264
+
265
+
Also: no tests whatsoever.
286
266
287
267
It also should be noted that *attrs* is not an all-or-nothing solution.
288
268
You can freely choose which features you want and disable those that you want more control over:
@@ -300,7 +280,7 @@ You can freely choose which features you want and disable those that you want mo
300
280
```
301
281
302
282
:::{admonition} Summary
303
-
If you don't care and like typing, we're not gonna stop you.
283
+
If you don't care and like the sound of your mechanical keyboard, we're not gonna stop you.
304
284
305
285
However it takes a lot of bias and determined rationalization to claim that *attrs* raises the mental burden on a project given how difficult it is to find the important bits in a hand-written class and how annoying it is to ensure you've copy-pasted your code correctly over all your classes.
0 commit comments