From bd53edb6d03ce4e811c8f319cff49ae6b5615114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Sat, 1 Jan 2022 20:03:02 +0100 Subject: [PATCH] Type annotations --- README.md | 12 +++++++++--- index.html | 14 +++++++++----- pdf/index_for_pdf.html | 4 ++-- pdf/index_for_pdf_print.html | 4 ++-- pdf/remove_links.py | 1 + 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index c732d68..74ed731 100644 --- a/README.md +++ b/README.md @@ -675,7 +675,7 @@ def f(): # def f(x, y): def f(): # def f(x=0, y=0): def f(, ): # def f(x, y=0): ``` -* **A function has it's default values evaluated when it's first encountered in the scope.** +* **A function has its default values evaluated when it's first encountered in the scope.** * **Any changes to mutable objects will persist between invocations.** @@ -1043,7 +1043,7 @@ class : : list/dict/set = field(default_factory=list/dict/set) ``` * **Objects can be made [sortable](#sortable) with `'order=True'` and immutable with `'frozen=True'`.** -* **For object to be hashable, all attributes must be hashable and frozen must be True.** +* **For object to be [hashable](#hashable), all attributes must be hashable and frozen must be True.** * **Function field() is needed because `': list = []'` would make a list that is shared among all instances. Its 'default_factory' argument can be any [callable](#callable).** * **For attributes of arbitrary type use `'typing.Any'`.** @@ -1055,6 +1055,13 @@ from dataclasses import make_dataclass = ('', [, ]) ``` +#### Rest of type annotations (CPython interpreter ignores them all): +```python +def func(: [= ]) -> : +: typing.List/Set/Iterable/Sequence/Optional[] +: typing.Dict/Tuple/Union[, ...] +``` + ### Slots **Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.** @@ -1068,7 +1075,6 @@ class MyClassWithSlots: ### Copy ```python from copy import copy, deepcopy - = copy() = deepcopy() ``` diff --git a/index.html b/index.html index ea13ff0..25dcf98 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -598,7 +598,7 @@ to_exclusive = <range>.stop
    -
  • A function has it's default values evaluated when it's first encountered in the scope.
  • +
  • A function has its default values evaluated when it's first encountered in the scope.
  • Any changes to mutable objects will persist between invocations.

#Splat Operator

Inside Function Call

Splat expands a collection into positional arguments, while splatty-splat expands a dictionary into keyword arguments.

args   = (1, 2)
@@ -886,7 +886,7 @@ Z = dataclasses.make_dataclass('Z', [
 
  • Objects can be made sortable with 'order=True' and immutable with 'frozen=True'.
  • -
  • For object to be hashable, all attributes must be hashable and frozen must be True.
  • +
  • For object to be hashable, all attributes must be hashable and frozen must be True.
  • Function field() is needed because '<attr_name>: list = []' would make a list that is shared among all instances. Its 'default_factory' argument can be any callable.
  • For attributes of arbitrary type use 'typing.Any'.
  • @@ -895,6 +895,11 @@ Z = dataclasses.make_dataclass('Z', ['<class_name>', <coll_of_tuples>) <tuple> = ('<attr_name>', <type> [, <default_value>])
    +

    Rest of type annotations (CPython interpreter ignores them all):

    def func(<arg_name>: <type> [= <obj>]) -> <type>:
    +<var_name>: typing.List/Set/Iterable/Sequence/Optional[<type>]
    +<var_name>: typing.Dict/Tuple/Union[<type>, ...]
    +
    +

    Slots

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

    class MyClassWithSlots:
         __slots__ = ['a']
         def __init__(self):
    @@ -903,7 +908,6 @@ Z = dataclasses.make_dataclass('Z', [

    Copy

    from copy import copy, deepcopy
    -
     <object> = copy(<object>)
     <object> = deepcopy(<object>)
     
    @@ -2876,7 +2880,7 @@ $ pyinstaller script.py --add-data '<path>:.'
    - +
    diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html index 2610191..365b8c1 100644 --- a/pdf/index_for_pdf.html +++ b/pdf/index_for_pdf.html @@ -132,7 +132,6 @@ struct module, 28-29
    subprocess module, 25
    super function, 14
    -synthesizer, 41
    sys module, 13, 21-22

    T

    table, 26, 27, 34, 37-38, 45-46
    @@ -140,7 +139,8 @@ threading module, 30
    time module, 34, 36
    tuples, 3, 4, 11
    -type, 4, 31-32

    +type, 4, 31-32
    +type annotations, 15

    W

    wave module, 40-41
    web, 36

    diff --git a/pdf/index_for_pdf_print.html b/pdf/index_for_pdf_print.html index 1b93c3f..3aa2bfe 100644 --- a/pdf/index_for_pdf_print.html +++ b/pdf/index_for_pdf_print.html @@ -132,7 +132,6 @@ struct module, 28-29
    subprocess module, 25
    super function, 14
    -synthesizer, 41
    sys module, 13, 21-22

    T

    table, 26, 27, 34, 37-38, 45-46
    @@ -140,7 +139,8 @@ threading module, 30
    time module, 34, 36
    tuples, 3, 4, 11
    -type, 4, 31-32

    +type, 4, 31-32
    +type annotations, 15

    W

    wave module, 40-41
    web, 36

    diff --git a/pdf/remove_links.py b/pdf/remove_links.py index 5480a8f..9ca06f5 100755 --- a/pdf/remove_links.py +++ b/pdf/remove_links.py @@ -12,6 +12,7 @@ MATCHES = { 'Adding \'!r\' before the colon converts object to string by calling its repr() method.': 'Adding \'!r\' before the colon converts object to string by calling its repr() method (p. 14).', 'It can be any callable, but is usually implemented as a function that returns a closure.': 'It can be any callable (p. 17), but is usually implemented as a function that returns a closure (p. 12).', 'Objects can be made sortable with \'order=True\' and immutable with \'frozen=True\'.': 'Objects can be made sortable with \'order=True\' and immutable with \'frozen=True\'.', + 'For object to be hashable, all attributes must be hashable and frozen must be True.': 'For object to be hashable, all attributes must be hashable and frozen must be True.', 'Function field() is needed because \'<attr_name>: list = []\' would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable.': 'Function field() is needed because \'<attr_name>: list = []\' would make a list that is shared among all instances. Its \'default_factory\' argument can be any callable (p. 17).', 'Sequence iterators returned by the iter() function, such as list_iterator and set_iterator.': 'Sequence iterators returned by the iter() function, such as list_iterator and set_iterator (p. 3).', 'Objects returned by the itertools module, such as count, repeat and cycle.': 'Objects returned by the itertools module, such as count, repeat and cycle (p. 3).',