You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Chapter1/python_new_features.ipynb
+133Lines changed: 133 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -216,6 +216,139 @@
216
216
"get_youngest_pet(pet_info2)"
217
217
]
218
218
},
219
+
{
220
+
"cell_type": "markdown",
221
+
"id": "e8a5659d",
222
+
"metadata": {},
223
+
"source": [
224
+
"### Enhance Code Readability with Python Dataclasses and Match Statements"
225
+
]
226
+
},
227
+
{
228
+
"cell_type": "markdown",
229
+
"id": "bb667ea2",
230
+
"metadata": {},
231
+
"source": [
232
+
"You can use Python dataclasses with Python match statements to create cleaner and more readable code. This approach can be particularly useful when setting conditions based on multiple attributes of a class, as it can simplify the code and make it easier to understand.\n"
233
+
]
234
+
},
235
+
{
236
+
"cell_type": "markdown",
237
+
"id": "50fdca57",
238
+
"metadata": {},
239
+
"source": [
240
+
"Using if-else:"
241
+
]
242
+
},
243
+
{
244
+
"cell_type": "code",
245
+
"execution_count": 11,
246
+
"id": "d2fb488f",
247
+
"metadata": {},
248
+
"outputs": [],
249
+
"source": [
250
+
"from dataclasses import dataclass\n",
251
+
"\n",
252
+
"@dataclass\n",
253
+
"class SubscriptionPlan:\n",
254
+
" name: str\n",
255
+
" price: float\n",
256
+
" unit: str\n",
257
+
"\n",
258
+
"def get_plan_details(plan):\n",
259
+
" if plan.name == \"basic\" and plan.unit == \"month\":\n",
260
+
" return f\"${plan.price} per month for one month.\"\n",
261
+
" elif plan.name == \"premium\" and plan.unit == \"year\":\n",
262
+
" return f\"${plan.price} per year for one year.\"\n",
263
+
" elif plan.name == \"\" and plan.price == 0.0 and plan.unit == \"\":\n",
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#write-union-types-as-x-y-in-python-3-10">2.10.3. Write Union Types as X|Y in Python 3.10</a></li>
518
-
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#walrus-operator-assign-a-variable-in-an-expression">2.10.4. Walrus Operator: Assign a Variable in an Expression</a></li>
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#enhance-code-readability-with-python-dataclasses-and-match-statements">2.10.3. Enhance Code Readability with Python Dataclasses and Match Statements</a></li>
518
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#write-union-types-as-x-y-in-python-3-10">2.10.4. Write Union Types as X|Y in Python 3.10</a></li>
519
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#walrus-operator-assign-a-variable-in-an-expression">2.10.5. Walrus Operator: Assign a Variable in an Expression</a></li>
<h2><spanclass="section-number">2.10.3. </span>Enhance Code Readability with Python Dataclasses and Match Statements<aclass="headerlink" href="#enhance-code-readability-with-python-dataclasses-and-match-statements" title="Permalink to this heading">#</a></h2>
644
+
<p>You can use Python dataclasses with Python match statements to create cleaner and more readable code. This approach can be particularly useful when setting conditions based on multiple attributes of a class, as it can simplify the code and make it easier to understand.</p>
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"$</span><spanclass="si">{</span><spanclass="n">plan</span><spanclass="o">.</span><spanclass="n">price</span><spanclass="si">}</span><spanclass="s2"> per month for one month."</span>
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"$</span><spanclass="si">{</span><spanclass="n">plan</span><spanclass="o">.</span><spanclass="n">price</span><spanclass="si">}</span><spanclass="s2"> per year for one year."</span>
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"$</span><spanclass="si">{</span><spanclass="n">price</span><spanclass="si">}</span><spanclass="s2"> per month for one month."</span>
<spanclass="k">return</span><spanclass="sa">f</span><spanclass="s2">"$</span><spanclass="si">{</span><spanclass="n">price</span><spanclass="si">}</span><spanclass="s2"> per year for one year."</span>
<h2><spanclass="section-number">2.10.3. </span>Write Union Types as X|Y in Python 3.10<aclass="headerlink" href="#write-union-types-as-x-y-in-python-3-10" title="Permalink to this heading">#</a></h2>
732
+
<h2><spanclass="section-number">2.10.4. </span>Write Union Types as X|Y in Python 3.10<aclass="headerlink" href="#write-union-types-as-x-y-in-python-3-10" title="Permalink to this heading">#</a></h2>
643
733
<p>Before Python 3.10, you need to use <codeclass="docutils literal notranslate"><spanclass="pre">typing.Union</span></code> to declare that a variable can have one of several different types.</p>
644
734
<divclass="cell docutils container">
645
735
<divclass="cell_input docutils container">
@@ -671,7 +761,7 @@ <h2><span class="section-number">2.10.3. </span>Write Union Types as X|Y in Pyth
<h2><spanclass="section-number">2.10.4. </span>Walrus Operator: Assign a Variable in an Expression<aclass="headerlink" href="#walrus-operator-assign-a-variable-in-an-expression" title="Permalink to this heading">#</a></h2>
764
+
<h2><spanclass="section-number">2.10.5. </span>Walrus Operator: Assign a Variable in an Expression<aclass="headerlink" href="#walrus-operator-assign-a-variable-in-an-expression" title="Permalink to this heading">#</a></h2>
675
765
<p>The walrus operator (<codeclass="docutils literal notranslate"><spanclass="pre">:=</span></code>) in Python 3.8 and above allows you to assign a variable in an expression. The walrus operator is useful when you want to:</p>
676
766
<ulclass="simple">
677
767
<li><p>Debug the components in an expression</p></li>
<h2><spanclass="section-number">2.10.5. </span>Fine-Grained Traceback in Python 3.11<aclass="headerlink" href="#fine-grained-traceback-in-python-3-11" title="Permalink to this heading">#</a></h2>
820
+
<h2><spanclass="section-number">2.10.6. </span>Fine-Grained Traceback in Python 3.11<aclass="headerlink" href="#fine-grained-traceback-in-python-3-11" title="Permalink to this heading">#</a></h2>
731
821
<p>Having a clear traceback makes it faster to debug your code. Python 3.11 provides fine-grained error locations in tracebacks, enabling developers to quickly identify the exact location of errors.</p>
732
822
<p>The following examples illustrate the difference in traceback between Python 3.9 and Python 3.11.</p>
733
823
<divclass="cell docutils container">
@@ -843,9 +933,10 @@ <h2><span class="section-number">2.10.5. </span>Fine-Grained Traceback in Python
843
933
<ulclass="visible nav section-nav flex-column">
844
934
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#simplify-conditional-execution-with-match-statements">2.10.1. Simplify Conditional Execution with Match Statements</a></li>
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#write-union-types-as-x-y-in-python-3-10">2.10.3. Write Union Types as X|Y in Python 3.10</a></li>
847
-
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#walrus-operator-assign-a-variable-in-an-expression">2.10.4. Walrus Operator: Assign a Variable in an Expression</a></li>
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#enhance-code-readability-with-python-dataclasses-and-match-statements">2.10.3. Enhance Code Readability with Python Dataclasses and Match Statements</a></li>
937
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#write-union-types-as-x-y-in-python-3-10">2.10.4. Write Union Types as X|Y in Python 3.10</a></li>
938
+
<liclass="toc-h2 nav-item toc-entry"><aclass="reference internal nav-link" href="#walrus-operator-assign-a-variable-in-an-expression">2.10.5. Walrus Operator: Assign a Variable in an Expression</a></li>
0 commit comments