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
{{ message }}
This repository was archived by the owner on Jul 2, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: src/basics/decorators.txt
+96-4Lines changed: 96 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -312,7 +312,7 @@ the function execution for some time:
312
312
from typing import Callable
313
313
314
314
315
-
def defer(seconds: int = 3) -> Callable:
315
+
def slow_down(seconds: int = 3) -> Callable:
316
316
def decorator(func: Callable) -> Callable:
317
317
def wrapper(*args, **kwargs):
318
318
started_at = time.perf_counter()
@@ -328,19 +328,111 @@ the function execution for some time:
328
328
return decorator
329
329
330
330
331
-
@defer()
331
+
@slow_down()
332
332
def function_a():
333
333
return 42
334
334
335
335
336
-
@defer(10)
336
+
@slow_down(10)
337
337
def function_b():
338
338
return 24
339
339
340
-
341
340
.. code-block::
342
341
343
342
function_a()
344
343
Completed in 3.00
345
344
function_b()
346
345
Completed in 10.00
346
+
347
+
Class decorators
348
+
================
349
+
350
+
There are some pre-defined decorators exists for usage together with classes.
351
+
They are:
352
+
353
+
- ``classmethod``
354
+
- ``staticmethod``
355
+
- ``property``
356
+
357
+
If you develop an intuitive understanding for their differences you’ll be able to write object-oriented Python that communicates its intent more clearly and will be easier to maintain in the long run :cite:`realpython:methods-demystified`.
358
+
359
+
Class methods
360
+
-------------
361
+
362
+
Instead of accepting a self parameter, class methods take a ``cls`` parameter
363
+
that points to the class—and not the object instance—when the method is called.
364
+
365
+
Because the class method only has access to this ``cls`` argument, it can't
366
+
modify object instance state. That would require access to ``self``.
367
+
However, class methods can still modify class state that applies across all
368
+
instances of the class.
369
+
370
+
The common usage for ``classmethod`` is provide alternative initializers.
371
+
372
+
Static methods
373
+
--------------
374
+
375
+
This type of method takes neither a ``self`` nor a ``cls`` parameter (but of
376
+
course it’s free to accept an arbitrary number of other parameters).
377
+
378
+
Therefore a static method can neither modify object state nor class state.
379
+
Static methods are restricted in what data they can access - and they're
380
+
primarily a way to namespace your methods.
381
+
382
+
It's tricky to explain ``staticmethod`` usage. Almost always you can create
383
+
a dedicated function instead of static method. But sometimes you need to
384
+
bind some logic independent from class itself or its instances to a class -
385
+
it common to encapsulate it with ``staticmethod``.
386
+
387
+
Properties
388
+
----------
389
+
390
+
It's a way to bind a method name to access it as an attribute. Properties are
391
+
"read-only" by default. This means a value cannot be assigned to ``property
0 commit comments