diff --git a/README.md b/README.md index 1e4e0b3..7ff3041 100644 --- a/README.md +++ b/README.md @@ -1020,6 +1020,26 @@ class C(A, B): pass [, , , ] ``` +### Property +**Pythonic way of implementing getters and setters.** +```python +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' +``` + ### Type Annotations * **They add type hints to variables, arguments and functions (`'def f() -> :'`).** * **Hints are used by type checkers like [mypy](https://pypi.org/project/mypy/), data validation libraries such as [Pydantic](https://pypi.org/project/pydantic/) and lately also by [Cython](https://pypi.org/project/Cython/) compiler. However, they are not enforced by CPython interpreter.** @@ -1053,26 +1073,6 @@ class : = ('', [, ]) ``` -### Property -**Pythonic way of implementing getters and setters.** -```python -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', reduces their memory footprint.** diff --git a/index.html b/index.html index 525ed3c..3c5ee02 100644 --- a/index.html +++ b/index.html @@ -859,6 +859,22 @@ 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)
+
+    @name.setter
+    def name(self, value):
+        self._name = value.split()
+
+ + +
>>> person = Person()
+>>> person.name = '\t Guido  van Rossum \n'
+>>> person.name
+'Guido van Rossum'
+

Type Annotations

  • They add type hints to variables, arguments and functions ('def f() -> <type>:').
  • Hints are used by type checkers like mypy, data validation libraries such as Pydantic and lately also by Cython compiler. However, they are not enforced by CPython interpreter.
  • @@ -889,22 +905,6 @@ Z = dataclasses.make_dataclass('Z', [<class> = make_dataclass('<class_name>', <coll_of_attribute_names>) <class> = make_dataclass('<class_name>', <coll_of_tuples>) <tuple> = ('<attr_name>', <type> [, <default_value>]) -

    Property

    Pythonic way of implementing getters and setters.

    class Person:
    -    @property
    -    def name(self):
    -        return ' '.join(self._name)
    -
    -    @name.setter
    -    def name(self, value):
    -        self._name = value.split()
    -
    - - -
    >>> 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):