diff --git a/README.md b/README.md index 92d08f3..3850297 100644 --- a/README.md +++ b/README.md @@ -1021,30 +1021,22 @@ class C(A, B): pass [, , , ] ``` -### Property -**Pythonic way of implementing getters and setters.** +### Type Annotations +* **They add type hints to variables, arguments and functions (`'def f() -> :'`).** +* **Are ignored by CPython interpreter, but used by tools such as [mypy](https://pypi.org/project/mypy/), [Pydantic](https://pypi.org/project/pydantic/) and [Cython](https://pypi.org/project/Cython/).** ```python -class Person: - @property - def name(self): - return ' '.join(self._name) - - @name.setter - def name(self, value): - self._name = value.split() -``` +from collections import abc -```python ->>> person = Person() ->>> person.name = '\t Guido van Rossum \n' ->>> person.name -'Guido van Rossum' +: [| ...] [= ] +: list/set[] [= ] +: abc.Iterable/abc.Sequence[] [= ] +: dict/tuple[, ...] [= ] ``` ### Dataclass -**Decorator that automatically generates init(), repr() and eq() special methods.** +**Decorator that uses class variables to generate init(), repr() and eq() special methods.** ```python -from dataclasses import dataclass, field +from dataclasses import dataclass, field, make_dataclass @dataclass(order=False, frozen=False) class : @@ -1057,24 +1049,34 @@ class : * **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'`.** -#### Inline: ```python -from dataclasses import make_dataclass = make_dataclass('', ) = make_dataclass('', ) = ('', [, ]) ``` -#### Rest of type annotations (CPython interpreter ignores them all): +### Property +**Pythonic way of implementing getters and setters.** ```python -import collections.abc as abc, typing as tp -: list/set/abc.Iterable/abc.Sequence/tp.Optional[] [= ] -: dict/tuple/tp.Union[, ...] [= ] -def func(: [= ]) -> : ... +class Person: + @property + def name(self): + return ' '.join(self._name) + + @name.setter + def name(self, value): + self._name = value.split() +``` + +```python +>>> person = Person() +>>> person.name = '\t Guido van Rossum \n' +>>> person.name +'Guido van Rossum' ``` ### Slots -**Mechanism that restricts objects to attributes listed in 'slots' and significantly reduces their memory footprint.** +**Mechanism that restricts objects to attributes listed in 'slots', reduces their memory footprint.** ```python class MyClassWithSlots: @@ -1086,8 +1088,7 @@ class MyClassWithSlots: ### Copy ```python from copy import copy, deepcopy - = copy() - = deepcopy() + = copy/deepcopy() ``` diff --git a/index.html b/index.html index 1ab83c4..ba06850 100644 --- a/index.html +++ b/index.html @@ -54,7 +54,7 @@
- +
@@ -860,23 +860,19 @@ Z = dataclasses.make_dataclass('Z', [>>> C.mro() [<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>] -

Property

Pythonic way of implementing getters and setters.

class Person:
-    @property
-    def name(self):
-        return ' '.join(self._name)
+

Type Annotations

    +
  • They add type hints to variables, arguments and functions ('def f() -> <type>:').
  • +
  • Are ignored by CPython interpreter, but used by tools such as mypy, Pydantic and Cython.
  • +
from collections import abc
 
-    @name.setter
-    def name(self, value):
-        self._name = value.split()
+<name>: <type> [| ...] [= <obj>]
+<name>: list/set[<type>] [= <obj>]
+<name>: abc.Iterable/abc.Sequence[<type>] [= <obj>]
+<name>: dict/tuple[<type>, ...] [= <obj>]
 
-
>>> person = Person()
->>> person.name = '\t Guido  van Rossum \n'
->>> person.name
-'Guido van Rossum'
-
-

Dataclass

Decorator that automatically generates init(), repr() and eq() special methods.

from dataclasses import dataclass, field
+

Dataclass

Decorator that uses class variables to generate init(), repr() and eq() special methods.

from dataclasses import dataclass, field, make_dataclass
 
 @dataclass(order=False, frozen=False)
 class <class_name>:
@@ -892,18 +888,26 @@ Z = dataclasses.make_dataclass('Z', [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'.
  • -

    Inline:

    from dataclasses import make_dataclass
    -<class> = make_dataclass('<class_name>', <coll_of_attribute_names>)
    +
    <class> = make_dataclass('<class_name>', <coll_of_attribute_names>)
     <class> = make_dataclass('<class_name>', <coll_of_tuples>)
    -<tuple> = ('<attr_name>', <type> [, <default_value>])
    +<tuple> = ('<attr_name>', <type> [, <default_value>])
    +

    Property

    Pythonic way of implementing getters and setters.

    class Person:
    +    @property
    +    def name(self):
    +        return ' '.join(self._name)
     
    -

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

    import collections.abc as abc, typing as tp
    -<var_name>: list/set/abc.Iterable/abc.Sequence/tp.Optional[<type>] [= <obj>]
    -<var_name>: dict/tuple/tp.Union[<type>, ...] [= <obj>]
    -def func(<arg_name>: <type> [= <obj>]) -> <type>: ...
    +    @name.setter
    +    def name(self, value):
    +        self._name = value.split()
     
    -

    Slots

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

    class MyClassWithSlots:
    +
    +
    >>> person = Person()
    +>>> person.name = '\t Guido  van Rossum \n'
    +>>> person.name
    +'Guido van Rossum'
    +
    +

    Slots

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

    class MyClassWithSlots:
         __slots__ = ['a']
         def __init__(self):
             self.a = 1
    @@ -911,8 +915,7 @@ Z = dataclasses.make_dataclass('Z', [

    Copy

    from copy import copy, deepcopy
    -<object> = copy(<object>)
    -<object> = deepcopy(<object>)
    +<object> = copy/deepcopy(<object>)
     

    #Duck Types

    A duck type is an implicit type that prescribes a set of special methods. Any object that has those methods defined is considered a member of that duck type.

    Comparable

      @@ -1162,7 +1165,7 @@ Hello World! index = members.index(member) + 1 return members[index % len(members)]
    -

    Inline

    Cutlery = Enum('Cutlery', 'FORK KNIFE SPOON')
    +

    Inline

    Cutlery = Enum('Cutlery', 'FORK KNIFE SPOON')
     Cutlery = Enum('Cutlery', ['FORK', 'KNIFE', 'SPOON'])
     Cutlery = Enum('Cutlery', {'FORK': 1, 'KNIFE': 2, 'SPOON': 3})
     
    @@ -2933,7 +2936,7 @@ $ deactivate # Deactivates the activ
    - +
    diff --git a/parse.js b/parse.js index fc94c7b..181ec93 100755 --- a/parse.js +++ b/parse.js @@ -87,7 +87,6 @@ const CONSTRUCTOR_OVERLOADING = ' self.a = a\n'; const DATACLASS = - 'from dataclasses import make_dataclass\n' + '<class> = make_dataclass(\'<class_name>\', <coll_of_attribute_names>)\n' + '<class> = make_dataclass(\'<class_name>\', <coll_of_tuples>)\n' + '<tuple> = (\'<attr_name>\', <type> [, <default_value>])';