From 853c09cc055ff065f48291d3594b8e5eb2523397 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Mon, 29 Jul 2019 17:10:45 +0200 Subject: [PATCH] Callable --- README.md | 6 ++++-- index.html | 10 +++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3c178a4..95ecca6 100644 --- a/README.md +++ b/README.md @@ -1035,7 +1035,7 @@ class : ``` * **An object can be made sortable with `'order=True'` or immutable with `'frozen=True'`.** * **Function field() is needed because `': list = []'` would make a list that is shared among all instances.** -* **Default_factory can be any callable.** +* **Default_factory can be any [callable](#callable).** ### Slots **Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.** @@ -1134,6 +1134,8 @@ class Counter: ``` ### Callable +* **All functions and classes have a call() method, hence are callable.** +* **When this cheatsheet uses `''` in an argument, it actually means `''`.** ```python class Counter: def __init__(self): @@ -1994,7 +1996,7 @@ with ThreadPoolExecutor(max_workers=None) as executor: results = executor.map(lambda x: x + 1, range(3)) # (1, 2, 3) results = executor.map(lambda x, y: x + y, 'abc', '123') # ('a1', 'b2', 'c3') ``` -* **CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map() unless passed function contains an I/O operation.** +* **CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map(), unless passed function contains an I/O operation.** Operator diff --git a/index.html b/index.html index 07020f5..c42d0db 100644 --- a/index.html +++ b/index.html @@ -988,7 +988,7 @@ Z = dataclasses.make_dataclass('Z', [
  • An object can be made sortable with 'order=True' or immutable with 'frozen=True'.
  • Function field() is needed because '<attr_name>: list = []' would make a list that is shared among all instances.
  • -
  • Default_factory can be any callable.
  • +
  • Default_factory can be any callable.
  • Slots

    Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.

    class MyClassWithSlots:
         __slots__ = ['a']
    @@ -1074,7 +1074,10 @@ Z = dataclasses.make_dataclass('Z', [>>> next(counter), next(counter), next(counter)
     (1, 2, 3)
     
    -

    Callable

    class Counter:
    +

    Callable

      +
    • All functions and classes have a call() method, hence are callable.
    • +
    • When this cheatsheet uses '<function>' in an argument, it actually means '<callable>'.
    • +
    class Counter:
         def __init__(self):
             self.i = 0
         def __call__(self):
    @@ -1082,6 +1085,7 @@ Z = dataclasses.make_dataclass('Z', [return self.i
     
    +
    >>> counter = Counter()
     >>> counter(), counter(), counter()
     (1, 2, 3)
    @@ -1752,7 +1756,7 @@ lock.release()
     
      -
    • CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map() unless passed function contains an I/O operation.
    • +
    • CPython interpreter can only run a single thread at the time. That is why this map() won't be faster than the standard map(), unless passed function contains an I/O operation.

    #Operator

    from operator import add, sub, mul, truediv, floordiv, mod, pow, neg, abs
     from operator import eq, ne, lt, le, gt, ge