<?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: Charlton Asino</title>
    <description>The latest articles on DEV Community by Charlton Asino (@charlton_a).</description>
    <link>https://dev.to/charlton_a</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%2F1155668%2F26dc3dfc-1fce-4f4b-8e81-7d7c96e90a1a.jpeg</url>
      <title>DEV Community: Charlton Asino</title>
      <link>https://dev.to/charlton_a</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/charlton_a"/>
    <language>en</language>
    <item>
      <title>Python Evaluates Assignments From Right To Left</title>
      <dc:creator>Charlton Asino</dc:creator>
      <pubDate>Wed, 27 Sep 2023 14:00:46 +0000</pubDate>
      <link>https://dev.to/charlton_a/python-evaluates-assignments-from-right-to-left-lem</link>
      <guid>https://dev.to/charlton_a/python-evaluates-assignments-from-right-to-left-lem</guid>
      <description>&lt;p&gt;&lt;strong&gt;Why does it matter?&lt;/strong&gt;&lt;br&gt;
Assigning variables in Python may range from simple statements such as &lt;code&gt;count=5&lt;/code&gt; to others that may require  evaluation such as &lt;code&gt;str_len=len("eat one piece of the fruit and you drown")&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;When assigning variables in Python the evaluation&lt;br&gt;
 is done from right to left.Meaning the length of the string &lt;code&gt;str_len&lt;/code&gt; will be evaluated first and then assigned.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tuples are immutable&lt;/strong&gt;&lt;br&gt;
When working with more complex objects the evaluation order may affect the behaviour of your objects.An example being tuples.Tuples are immutable in Python in that they cannot be modified once created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=(&lt;/span&gt;1, &lt;span class="o"&gt;[&lt;/span&gt;2,3,4]&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;a&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;lt;class 'tuple'&amp;gt;&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a[0]&lt;span class="o"&gt;=&lt;/span&gt;8
&lt;span class="go"&gt;Traceback (most recent call last):
&lt;/span&gt;&lt;span class="gp"&gt;  File "&amp;lt;stdin&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;", line 1, in &amp;lt;module&amp;gt;
&lt;/span&gt;&lt;span class="go"&gt;TypeError: 'tuple' object does not support item assignment
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a[1]&lt;span class="o"&gt;=[&lt;/span&gt;5,6,7,8]
&lt;span class="go"&gt;Traceback (most recent call last):
&lt;/span&gt;&lt;span class="gp"&gt;  File "&amp;lt;stdin&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;", line 1, in &amp;lt;module&amp;gt;
&lt;/span&gt;&lt;span class="go"&gt;TypeError: 'tuple' object does not support item assignment
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a
&lt;span class="go"&gt;(1, [2, 3, 4])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modifying tuple &lt;code&gt;a&lt;/code&gt; raises an exception and inspecting it after shows that the tuple has not been modified.&lt;br&gt;
&lt;strong&gt;Murky waters&lt;/strong&gt;&lt;br&gt;
Depending on how  a tuple is operated on and its composition, you can get some unexpected behaviour(they become mutable). This can occur when using &lt;code&gt;augmented assignment operators&lt;/code&gt; in particular &lt;code&gt;__iadd__ (in-place addition)&lt;/code&gt; which is commonly used as &lt;code&gt;+=&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a
&lt;span class="go"&gt;(1, [2, 3, 4])
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a[1]+&lt;span class="o"&gt;=[&lt;/span&gt;100,101,700]
&lt;span class="go"&gt;Traceback (most recent call last):
&lt;/span&gt;&lt;span class="gp"&gt;  File "&amp;lt;stdin&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;", line 1, in &amp;lt;module&amp;gt;
&lt;/span&gt;&lt;span class="go"&gt;TypeError: 'tuple' object does not support item assignment
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An attempt to modify the tuple raises an exception which conforms to the expected behaviour that tuples are immutable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a
&lt;span class="go"&gt;(1, [2, 3, 4])
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a[1]+&lt;span class="o"&gt;=[&lt;/span&gt;100,101,700]
&lt;span class="go"&gt;Traceback (most recent call last):
&lt;/span&gt;&lt;span class="gp"&gt;  File "&amp;lt;stdin&amp;gt;&lt;/span&gt;&lt;span class="s2"&gt;", line 1, in &amp;lt;module&amp;gt;
&lt;/span&gt;&lt;span class="go"&gt;TypeError: 'tuple' object does not support item assignment
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; a
&lt;span class="go"&gt;(1, [2, 3, 4, 100, 101, 700])
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But on inspection as per the above example.The tuple has been modified ,the list in the tuple in particular even though we got an exception.&lt;/p&gt;

&lt;p&gt;This occurred due to two aspects of the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In-place addition which is supported in lists.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Evaluation of  assignments from right to left.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;a[1]&lt;/code&gt; is a list &lt;code&gt;[2, 3, 4]&lt;/code&gt; which supports inplace addition .The statement &lt;code&gt;a[1]+=[100,101,700]&lt;/code&gt; is &lt;code&gt;evaluated from right to left&lt;/code&gt; meaning the list at &lt;code&gt;a[1]&lt;/code&gt;  is modified first then a assigned into the tuple and this raises an exception.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Take away&lt;/strong&gt;&lt;br&gt;
Understading how python evaluates statements is important, especially when performing operations on  objects which contain types with different behaviours.  &lt;/p&gt;

</description>
      <category>python</category>
      <category>performance</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Python Objects As Dictionary Keys</title>
      <dc:creator>Charlton Asino</dc:creator>
      <pubDate>Thu, 07 Sep 2023 13:34:20 +0000</pubDate>
      <link>https://dev.to/charlton_a/python-objects-as-dictonary-keys-2jo9</link>
      <guid>https://dev.to/charlton_a/python-objects-as-dictonary-keys-2jo9</guid>
      <description>&lt;p&gt;&lt;strong&gt;When can it happen?&lt;/strong&gt;&lt;br&gt;
You have a custom class(object) and you want to store related metadata in memory . You decide to use Python dict and leverage the faster look ups O(1)  . &lt;br&gt;
In the case of a user object:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94g5xbgfd35ujlp479ir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F94g5xbgfd35ujlp479ir.png" alt="user class"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Python dicts ,eq and the hash function&lt;/strong&gt;&lt;br&gt;
For the user object , tracking the number of clicks made the user is required for later computations.The object will be the key for our dict and the number of clicks will be the value .In this case the user will be instatiated more than once.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;

&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;1,&lt;span class="s2"&gt;"Jill"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;clicks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dict&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u1]&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1 Jill)&amp;gt;&lt;/span&gt;: 1&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;2,&lt;span class="s2"&gt;"Lock"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u2]&lt;span class="o"&gt;=&lt;/span&gt;2
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1 Jill)&amp;gt;&lt;/span&gt;: 1, &amp;lt;User&lt;span class="o"&gt;(&lt;/span&gt;2 Lock&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;: 2&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;1,&lt;span class="s2"&gt;"Jill"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u3]&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1 Jill)&amp;gt;&lt;/span&gt;: 1, &amp;lt;User&lt;span class="o"&gt;(&lt;/span&gt;2 Lock&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;: 2, &amp;lt;User&lt;span class="o"&gt;(&lt;/span&gt;1 Jill&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;: 3&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="go"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Both &lt;code&gt;u1&lt;/code&gt; and&lt;code&gt;u3&lt;/code&gt;  are representations of the same user ,Jill with the same user_id 1 but when updating the value for &lt;code&gt;u3&lt;/code&gt;a new key is created.&lt;/p&gt;

&lt;p&gt;This happens because Python uses the object id to generate a hash which is used as a key in the dict.Both &lt;code&gt;u1&lt;/code&gt;and &lt;code&gt;u3&lt;/code&gt; have different object ids hence will have different dictionary keys despite representing the same user.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;

&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;u1&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;140030203095248
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;id&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;u3&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="go"&gt;140030203095440
&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; 
&lt;span class="go"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;code&gt; &amp;lt;User(1 Jill)&amp;gt;&lt;/code&gt;value needs to be  updated for this case since it already exists in the dictionary .To a achieve this a &lt;code&gt;__hash__&lt;/code&gt; and&lt;code&gt;__eq__&lt;/code&gt; dunder methods should be implented for the user object where if the objects are equal they will have the same hash.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxgvqzpstwx5hvbuilwr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdxgvqzpstwx5hvbuilwr.png" alt="user class with eq and hash"&gt;&lt;/a&gt;&lt;br&gt;
Creating  a dict with the user updated object gives the required output.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight console"&gt;&lt;code&gt;&lt;span class="go"&gt;

&lt;/span&gt;&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;1,&lt;span class="s2"&gt;"Jill"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;clicks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;dict&lt;span class="o"&gt;()&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u1]&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1,Jill)&amp;gt;&lt;/span&gt;: 1&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;2,&lt;span class="s2"&gt;"Lock"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u2]&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1,Jill)&amp;gt;&lt;/span&gt;: 1, &amp;lt;User&lt;span class="o"&gt;(&lt;/span&gt;2,Lock&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;: 1&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;u3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;User&lt;span class="o"&gt;(&lt;/span&gt;1,&lt;span class="s2"&gt;"Jill"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks[u3]&lt;span class="o"&gt;=&lt;/span&gt;3
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; clicks
&lt;span class="gp"&gt;{&amp;lt;User(1,Jill)&amp;gt;&lt;/span&gt;: 3, &amp;lt;User&lt;span class="o"&gt;(&lt;/span&gt;2,Lock&lt;span class="o"&gt;)&amp;gt;&lt;/span&gt;: 1&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="gp"&gt;&amp;gt;&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="go"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Upating the&lt;code&gt;clicks&lt;/code&gt;dict with the object &lt;code&gt;u3&lt;/code&gt; which is represents the user Jill with user_id 1 ,updates the existing object in the dict &lt;code&gt;u1&lt;/code&gt; which is respresents the same user.&lt;/p&gt;

</description>
      <category>python</category>
      <category>tutorial</category>
      <category>programming</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
