diff --git a/README.md b/README.md index c906e61..4f2da95 100644 --- a/README.md +++ b/README.md @@ -995,29 +995,31 @@ Z = make_dataclass('Z', ['a']); print/str/repr(Z()) >>> ``` -### Inheritance +### Subclass +* **Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.** +* **Subclass can then add its own methods and attributes or override inherited ones by reusing their names.** + ```python class Person: def __init__(self, name): self.name = name + def __repr__(self): + return f'Person({self.name!r})' + def __lt__(self, other): + return self.name < other.name class Employee(Person): def __init__(self, name, staff_num): super().__init__(name) self.staff_num = staff_num + def __repr__(self): + return f'Employee({self.name!r}, {self.staff_num})' ``` -#### Multiple inheritance: -```python -class A: pass -class B: pass -class C(A, B): pass -``` - -**MRO determines the order in which parent classes are traversed when searching for a method or an attribute:** ```python ->>> C.mro() -[, , , ] +>>> people = {Person('Ann'), Employee('Bob', 0)} +>>> sorted(people) +[Person('Ann'), Employee('Bob', 0)] ``` ### Type Annotations @@ -1685,8 +1687,8 @@ from pathlib import Path = .parent # Returns Path without the final component. = .name # Returns final component as a string. = .suffix # Returns name's last extension, e.g. '.py'. - = .stem # Returns name without last extension. - = .parts # Returns all path's components as strings. + = .stem # Returns name without the last extension. + = .parts # Returns all components as strings. ``` ```python @@ -1820,10 +1822,10 @@ import csv = next() # Returns next row as a list of strings. = list() # Returns a list of remaining rows. ``` -* **File must be opened with a `'newline=""'` argument, or newlines embedded inside quoted fields will not be interpreted correctly!** +* **File must be opened with a `'newline=""'` argument, or all '\r\n' sequences inside quoted fields will get converted to '\n'!** * **To print the spreadsheet to the console use [Tabulate](#table) library.** * **For XML and binary Excel files (xlsx, xlsm and xlsb) use [Pandas](#dataframe-plot-encode-decode) library.** -* **Reader accepts any iterator of strings, not just files.** +* **Reader accepts any collection of strings, not just files.** ### Write ```python diff --git a/index.html b/index.html index d2af9dd..a26d191 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@
- +
@@ -849,25 +849,30 @@ Z = make_dataclass('Z', [>>> <obj> -

Inheritance

class Person:
+

Subclass

    +
  • Inheritance is a mechanism that enables a class to extend another class (subclass to extend its parent), and by doing so inherit all its methods and attributes.
  • +
  • Subclass can then add its own methods and attributes or override inherited ones by reusing their names.
  • +
class Person:
     def __init__(self, name):
         self.name = name
+    def __repr__(self):
+        return f'Person({self.name!r})'
+    def __lt__(self, other):
+        return self.name < other.name
 
 class Employee(Person):
     def __init__(self, name, staff_num):
         super().__init__(name)
         self.staff_num = staff_num
+    def __repr__(self):
+        return f'Employee({self.name!r}, {self.staff_num})'
 
-

Multiple inheritance:

class A: pass
-class B: pass
-class C(A, B): pass
-
-

MRO determines the order in which parent classes are traversed when searching for a method or an attribute:

-
>>> C.mro()
-[<class 'C'>, <class 'A'>, <class 'B'>, <class 'object'>]
-
+
>>> people = {Person('Ann'), Employee('Bob', 0)}
+>>> sorted(people)
+[Person('Ann'), Employee('Bob', 0)]
+

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.
  • @@ -1431,8 +1436,8 @@ args = p.parse_args() <Path> = <Path>.parent # Returns Path without the final component. <str> = <Path>.name # Returns final component as a string. <str> = <Path>.suffix # Returns name's last extension, e.g. '.py'. -<str> = <Path>.stem # Returns name without last extension. -<tup.> = <Path>.parts # Returns all path's components as strings. +<str> = <Path>.stem # Returns name without the last extension. +<tup.> = <Path>.parts # Returns all components as strings.
<iter> = <Path>.iterdir()           # Returns directory contents as Path objects.
 <iter> = <Path>.glob('<pattern>')   # Returns Paths matching the wildcard pattern.
@@ -1522,10 +1527,10 @@ CompletedProcess(args=['bc', 'newline=""' argument, or newlines embedded inside quoted fields will not be interpreted correctly!
+
  • File must be opened with a 'newline=""' argument, or all '\r\n' sequences inside quoted fields will get converted to '\n'!
  • To print the spreadsheet to the console use Tabulate library.
  • For XML and binary Excel files (xlsx, xlsm and xlsb) use Pandas library.
  • -
  • Reader accepts any iterator of strings, not just files.
  • +
  • Reader accepts any collection of strings, not just files.
  • Write

    <writer> = csv.writer(<file>)       # Also: `dialect='excel', delimiter=','`.
     <writer>.writerow(<collection>)     # Encodes objects using `str(<el>)`.
    @@ -2940,7 +2945,7 @@ $ deactivate                # Deactivates the active
      
     
       
     
    diff --git a/pdf/index_for_pdf.html b/pdf/index_for_pdf.html
    index 9cb299e..4327865 100644
    --- a/pdf/index_for_pdf.html
    +++ b/pdf/index_for_pdf.html
    @@ -131,7 +131,7 @@
     strings, 4-7, 14
    struct module, 28-29
    subprocess module, 25
    -super function, 14
    +super function, 14
    sys module, 13, 21-22

    T

    table, 26, 27, 34, 37-38, 45-46