<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Justice Cofie</title>
    <description>The latest articles on DEV Community by Justice Cofie (@justicebrains).</description>
    <link>https://dev.to/justicebrains</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F430533%2Ff953b90d-4799-4c24-aa5e-89d2cba79945.png</url>
      <title>DEV Community: Justice Cofie</title>
      <link>https://dev.to/justicebrains</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justicebrains"/>
    <language>en</language>
    <item>
      <title>The Power Of Python's Assert Statement</title>
      <dc:creator>Justice Cofie</dc:creator>
      <pubDate>Tue, 16 Aug 2022 08:08:41 +0000</pubDate>
      <link>https://dev.to/justicebrains/the-power-of-pythons-assert-statement-2aej</link>
      <guid>https://dev.to/justicebrains/the-power-of-pythons-assert-statement-2aej</guid>
      <description>&lt;h2&gt;
  
  
  What Are Assertions In Python?
&lt;/h2&gt;

&lt;p&gt;In Python, assertions are statements that allow developers to test the correctness of their code. If the assertion evaluates to true, nothing happens; otherwise, an &lt;em&gt;AssertionError&lt;/em&gt; is raised.&lt;/p&gt;

&lt;p&gt;Let's look at how assertions are used in Python first, but first let's create a Book class. Below is an example Python code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from typing import NamedTuple

class Book(NamedTuple):
    author: str
    title: str
    published_year : int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We created a Book class that inherits from the typing module's NamedTuple class. Because NamedTuples are tuples by default, our Book class is immutable.&lt;br&gt;
Creating an instance of the Book class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; book1 = Book('Christian Mayer', 'The Art Of Clean Code', 2022)
&amp;gt;&amp;gt;&amp;gt; book1
Book(author='Christian Mayer', title='The Art Of Clean Code', published_year=2022)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use the assert statement to test for the truthiness of the published_year.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; assert(book1.published_year == "2022")
Traceback (most recent call last):
  File "&amp;lt;stdin&amp;gt;", line 1, in &amp;lt;module&amp;gt;
AssertionError
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;AssertionError was raised because the published_year has an integer type and we tested it against a string.&lt;/p&gt;

&lt;p&gt;Now let's test it with a valid year value of 2022&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; assert(book1.published_year == 2022)
&amp;gt;&amp;gt;&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because, the assertion statement is true nothing happened&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Use Assertions?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;As a debugging aid rather than an error handling mechanism&lt;/li&gt;
&lt;li&gt;As internal self-checks for a program &lt;/li&gt;
&lt;li&gt;Allows developers to find the likely root cause of a bug more quickly&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>assert</category>
      <category>debug</category>
    </item>
  </channel>
</rss>
