From d6e371c7d388b08a3e85d469975fee7857cdf260 Mon Sep 17 00:00:00 2001 From: Chaiyawut Thengket Date: Tue, 15 Aug 2023 15:20:49 +0700 Subject: [PATCH 1/2] add howto for self-referencing type hints --- self-referencing-hints.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 self-referencing-hints.md diff --git a/self-referencing-hints.md b/self-referencing-hints.md new file mode 100644 index 0000000..1a50cda --- /dev/null +++ b/self-referencing-hints.md @@ -0,0 +1,18 @@ +## Self-Referencing Type Hints + +Inside of a class, if you write a type hint that refers to the same class Python will raise an Error because the class is not known yet. +For example: +```python +class Money: + def __add__(self, other: Money) -> Money: # ERROR: Money is unknown + """Return the sum of two money objects.""" + ... (code omitted) +``` + +to fix this, add the following import: +```python +from __future__ import annotations + +class Money: + def __add__(self, other: Money) -> Money: # OK! +``` \ No newline at end of file From 3f3bed3ffb4009018557555bfc36ea4bfdfee05f Mon Sep 17 00:00:00 2001 From: Chaiyawut Thengket <114904790+ChaiyawutTar@users.noreply.github.com> Date: Tue, 15 Aug 2023 15:59:45 +0700 Subject: [PATCH 2/2] Update README.md --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index a337027..13832ad 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,11 @@ ## Python How-To Examples -by Chaiyawut Thengket \ No newline at end of file +by Chaiyawut Thengket + +we +rtferw +gtewr +tgerw +tgerw +t +ewt +wre