|
204 | 204 | "- [setup.py](#setuppy)\n", |
205 | 205 | "- [Dataclasses](#dataclasses)\n", |
206 | 206 | " - [Features](#features)\n", |
| 207 | + " - [Default values](#default-values)\n", |
| 208 | + " - [Type hints](#type-hints)\n", |
207 | 209 | "- [Virtual Environment](#virtual-environment)\n", |
208 | 210 | " - [virtualenv](#virtualenv)\n", |
209 | 211 | " - [pipenv](#pipenv)\n", |
|
846 | 848 | "\n", |
847 | 849 | "| Expression | Evaluates to |\n", |
848 | 850 | "| ----------------- | ------------ |\n", |
849 | | - "| `True and True` | `True` |\n", |
850 | | - "| `True and False` | `False` |\n", |
851 | | - "| `False and True` | `False` |\n", |
852 | | - "| `False and False` | `False` |\n", |
| 851 | + "| `True and True` | `True` |\n", |
| 852 | + "| `True and False` | `False` |\n", |
| 853 | + "| `False and True` | `False` |\n", |
| 854 | + "| `False and False` | `False` |\n", |
853 | 855 | "\n", |
854 | 856 | "The *or* Operator’s *Truth* Table:\n", |
855 | 857 | "\n", |
856 | | - "| Expression | Evaluates to |\n", |
857 | | - "| ---------------- | -------------- |\n", |
858 | | - "| `True or True` | `True` |\n", |
859 | | - "| `True or False` | `True` |\n", |
860 | | - "| `False or True` | `True` |\n", |
861 | | - "| `False or False` | `False` |\n", |
| 858 | + "| Expression | Evaluates to |\n", |
| 859 | + "| ---------------- | ------------ |\n", |
| 860 | + "| `True or True` | `True` |\n", |
| 861 | + "| `True or False` | `True` |\n", |
| 862 | + "| `False or True` | `True` |\n", |
| 863 | + "| `False or False` | `False` |\n", |
862 | 864 | "\n", |
863 | 865 | "The *not* Operator’s *Truth* Table:\n", |
864 | 866 | "\n", |
865 | | - "| Expression | Evaluates to |\n", |
866 | | - "| ------------ | ------------- |\n", |
867 | | - "| `not True` | `False` |\n", |
868 | | - "| `not False` | `True` |\n", |
| 867 | + "| Expression | Evaluates to |\n", |
| 868 | + "| ----------- | ------------ |\n", |
| 869 | + "| `not True` | `False` |\n", |
| 870 | + "| `not False` | `True` |\n", |
869 | 871 | "\n", |
870 | 872 | "[*Return to the Top*](#python-cheatsheet)\n", |
871 | 873 | "\n", |
|
3869 | 3871 | "\n", |
3870 | 3872 | "### Escape Characters\n", |
3871 | 3873 | "\n", |
3872 | | - "| Escape character | Prints as |\n", |
3873 | | - "| ------------------ | -------------------- |\n", |
3874 | | - "| `\\'` | Single quote |\n", |
3875 | | - "| `\\\"` | Double quote |\n", |
3876 | | - "| `\\t` | Tab |\n", |
3877 | | - "| `\\n` | Newline (line break) |\n", |
3878 | | - "| `\\\\` | Backslash |\n", |
| 3874 | + "| Escape character | Prints as |\n", |
| 3875 | + "| ---------------- | -------------------- |\n", |
| 3876 | + "| `\\'` | Single quote |\n", |
| 3877 | + "| `\\\"` | Double quote |\n", |
| 3878 | + "| `\\t` | Tab |\n", |
| 3879 | + "| `\\n` | Newline (line break) |\n", |
| 3880 | + "| `\\\\` | Backslash |\n", |
3879 | 3881 | "\n", |
3880 | 3882 | "Example:" |
3881 | 3883 | ] |
|
5435 | 5437 | "| `\\d`, `\\w`, and `\\s` | a digit, word, or space character, ectively. |\n", |
5436 | 5438 | "| `\\D`, `\\W`, and `\\S` | anything except a digit, word, or space acter, respectively. |\n", |
5437 | 5439 | "| `[abc]` | any character between the brackets (such as a, b, ). |\n", |
5438 | | - "| `[^abc]` | any character that isn’t between the brackets. |\n", |
| 5440 | + "| `[^abc]` | any character that isn’t between the brackets. |\n", |
5439 | 5441 | "\n", |
5440 | 5442 | "[*Return to the Top*](#python-cheatsheet)\n", |
5441 | 5443 | "\n", |
|
7057 | 7059 | "| ---------- | -------------------- | ------------------------------------------------------------------------------------------------------------------------------ |\n", |
7058 | 7060 | "| `DEBUG` | `logging.debug()` | The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems. |\n", |
7059 | 7061 | "| `INFO` | `logging.info()` | Used to record information on general events in your program or confirm that things are working at their point in the program. |\n", |
7060 | | - "| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |\n", |
| 7062 | + "| `WARNING` | `logging.warning()` | Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future. |\n", |
7061 | 7063 | "| `ERROR` | `logging.error()` | Used to record an error that caused the program to fail to do something. |\n", |
7062 | 7064 | "| `CRITICAL` | `logging.critical()` | The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely. |\n", |
7063 | 7065 | "\n", |
|
7648 | 7650 | "2" |
7649 | 7651 | ] |
7650 | 7652 | }, |
| 7653 | + { |
| 7654 | + "cell_type": "markdown", |
| 7655 | + "metadata": {}, |
| 7656 | + "source": [ |
| 7657 | + "[*Return to the Top*](#python-cheatsheet)\n", |
| 7658 | + "\n", |
| 7659 | + "### Default values\n", |
| 7660 | + "\n", |
| 7661 | + "It is easy to add default values to the fields of your data class." |
| 7662 | + ] |
| 7663 | + }, |
| 7664 | + { |
| 7665 | + "cell_type": "code", |
| 7666 | + "execution_count": null, |
| 7667 | + "metadata": {}, |
| 7668 | + "outputs": [], |
| 7669 | + "source": [ |
| 7670 | + ">>> @dataclass\n", |
| 7671 | + "... class Product:\n", |
| 7672 | + "... name: str\n", |
| 7673 | + "... count: int = 0\n", |
| 7674 | + "... price: float = 0.0\n", |
| 7675 | + "...\n", |
| 7676 | + ">>> obj = Product(\"Python\")\n", |
| 7677 | + ">>> obj.name\n", |
| 7678 | + "Python\n", |
| 7679 | + ">>> obj.count\n", |
| 7680 | + "0\n", |
| 7681 | + ">>> obj.price\n", |
| 7682 | + "0.0" |
| 7683 | + ] |
| 7684 | + }, |
| 7685 | + { |
| 7686 | + "cell_type": "markdown", |
| 7687 | + "metadata": {}, |
| 7688 | + "source": [ |
| 7689 | + "### Type hints\n", |
| 7690 | + "\n", |
| 7691 | + "It is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use ```typing.Any```." |
| 7692 | + ] |
| 7693 | + }, |
| 7694 | + { |
| 7695 | + "cell_type": "code", |
| 7696 | + "execution_count": null, |
| 7697 | + "metadata": {}, |
| 7698 | + "outputs": [], |
| 7699 | + "source": [ |
| 7700 | + ">>> from dataclasses import dataclass\n", |
| 7701 | + ">>> from typing import Any\n", |
| 7702 | + "\n", |
| 7703 | + ">>> @dataclass\n", |
| 7704 | + "... class WithoutExplicitTypes:\n", |
| 7705 | + "... name: Any\n", |
| 7706 | + "... value: Any = 42\n", |
| 7707 | + "..." |
| 7708 | + ] |
| 7709 | + }, |
7651 | 7710 | { |
7652 | 7711 | "cell_type": "markdown", |
7653 | 7712 | "metadata": {}, |
|
0 commit comments