<?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>Forem: aabidsofi19</title>
    <description>The latest articles on Forem by aabidsofi19 (@aabidsofi).</description>
    <link>https://forem.com/aabidsofi</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%2F717019%2Fed1135c1-b6b1-4929-b939-e8e9f097779e.png</url>
      <title>Forem: aabidsofi19</title>
      <link>https://forem.com/aabidsofi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/aabidsofi"/>
    <language>en</language>
    <item>
      <title>Python Anti-pattern you should avoid .</title>
      <dc:creator>aabidsofi19</dc:creator>
      <pubDate>Sun, 24 Jul 2022 12:59:19 +0000</pubDate>
      <link>https://forem.com/aabidsofi/python-anti-pattern-you-should-avoid--5g3j</link>
      <guid>https://forem.com/aabidsofi/python-anti-pattern-you-should-avoid--5g3j</guid>
      <description>&lt;h2&gt;
  
  
  Python Antipattern :  Impure(IO) Default Arguments
&lt;/h2&gt;

&lt;p&gt;One of most liked features of python functions is default arguments  . We use them &lt;br&gt;
every day . But there is gotcha that you need  to take care . &lt;/p&gt;

&lt;p&gt;Function default arguments are evaluated at module load time rather than function invocation time .&lt;br&gt;
Which helps with perfomance benefits when the default arguments are static and deterministic .&lt;/p&gt;

&lt;p&gt;So if your function is non-deterministic or has side-effects, you could run into trouble.&lt;br&gt;
As the example shows, the printed time is frozen; it was set once when the code was first evaluated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# Antitpattern and  buggy
&lt;/span&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()):&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;2021&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;43.009959&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="mi"&gt;2021&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;09&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;43.009959&lt;/span&gt;

&lt;span class="c1"&gt;# do this instead 
&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="bp"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="bp"&gt;None&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;dt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2022&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;07&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;07&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;16.435203&lt;/span&gt;
&lt;span class="n"&gt;current_datetime&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;2022&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;07&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;24&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;08&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mf"&gt;16.635203&lt;/span&gt;


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

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Books to Advance Your Python Skills</title>
      <dc:creator>aabidsofi19</dc:creator>
      <pubDate>Sat, 18 Jun 2022 06:52:54 +0000</pubDate>
      <link>https://forem.com/aabidsofi/books-to-advance-your-python-skills-5k3</link>
      <guid>https://forem.com/aabidsofi/books-to-advance-your-python-skills-5k3</guid>
      <description>&lt;p&gt;If you have stumbled to this post .. I can bet you love python ... And when you love&lt;br&gt;
something you should take care of it . in this small blog post i have compiled a&lt;br&gt;
list of books that have helped me become a better python programmer .&lt;/p&gt;

&lt;p&gt;Note : The list is not in any particular order and there are no affliate links&lt;br&gt;
either :) .&lt;/p&gt;

&lt;p&gt;We all agree with the fact that python is an easy to learn language . But the&lt;br&gt;
best thing that I have noticed while coding in python is how well it does a job&lt;br&gt;
of hiding  the unnecessary abstraction yet providing the programmer with the&lt;br&gt;
most control .&lt;/p&gt;

&lt;p&gt;In this section I have listed the books that will help you better understand&lt;br&gt;
python . Which will help  you to make  better decisions next time working on something&lt;br&gt;
whether it is which datastructure should I use or in which way the code will be&lt;br&gt;
more maintainable ..&lt;/p&gt;

&lt;p&gt;Lets dive in :&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Fluent Python - by Luciano Ramalho .
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7owwBTHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfk4h13lbczr9r5epl87.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7owwBTHh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vfk4h13lbczr9r5epl87.jpg" alt="Image description" width="381" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to become more productive  with python and you can choose&lt;br&gt;
only one book .. This is it .&lt;/p&gt;

&lt;p&gt;Written by Luciano Ramalho . This book will open the multiverse of python for&lt;br&gt;
you .&lt;/p&gt;

&lt;p&gt;I bet on this that you  will get an Aha .. moment reading every single page of&lt;br&gt;
it ..  summing in its own title  it will make you fluent in python . and will&lt;br&gt;
help you make smarter  decisions next time you are writing python.&lt;/p&gt;

&lt;p&gt;It is nicely structured , starting with easier to get topics and progressively&lt;br&gt;
going to more advanced topics like metaprogramming and  concurrency .&lt;/p&gt;

&lt;p&gt;Who it is not for :&lt;/p&gt;

&lt;p&gt;If are a programmer who has just started with python . This book won't serve the&lt;br&gt;
purpose best and will just make the journey a little harder .&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Programming Python -by Mark Lutz .
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3x9ZW8uS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl9u08emtg0gg2qpzyqw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3x9ZW8uS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tl9u08emtg0gg2qpzyqw.jpg" alt="Image description" width="381" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another Gem . This book serves a an indepth guide to python and how to use it&lt;br&gt;
for different usecases. Rather  than being a book only about the language&lt;br&gt;
sematics this book does a good job at making  usecases of python sound and clear&lt;br&gt;
.&lt;/p&gt;

&lt;p&gt;This book is divided into  sections like Introduction to python , Gui&lt;br&gt;
Programming , Web Programming ,etc .&lt;/p&gt;

&lt;p&gt;I love this book mostly because it has helped me to understand the vastness of&lt;br&gt;
the python ecosystem . And giving an eagles eye view of  different domains with python.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Python Cookbook
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7rEygkf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3x839maaq7eu2gqg15m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7rEygkf2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a3x839maaq7eu2gqg15m.jpg" alt="Image description" width="880" height="1129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This book has a recipe for everything python .  David Beazley is a well renowed&lt;br&gt;
name in the python community . And Python Cookbook is his masterpiece.&lt;/p&gt;

&lt;p&gt;This is not a book of tricks but a guide to solve problem in pythonic manner .&lt;br&gt;
David does a good job of not only explaining the solution  but also having a&lt;br&gt;
openended discussion about the drawbacks and edgecases ..&lt;/p&gt;

&lt;p&gt;I have already read it 2 -3 times yet and continuoly look for  day to day python&lt;br&gt;
problems in it .. I never upsets :)&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Robust Python
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OsMPgiYc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t49czgf94esl2w0j50x.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OsMPgiYc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t49czgf94esl2w0j50x.jpg" alt="Image description" width="381" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Writing code at  industry level scale is no fun and games .  Your code is going&lt;br&gt;
to get changed  and bashed a lot and surely is not immune  to bugs .&lt;br&gt;
And nothing can stop it .&lt;/p&gt;

&lt;p&gt;Writing clean code and refactoring is one the most important skills to write&lt;br&gt;
scalable and  better .&lt;/p&gt;

&lt;p&gt;This book just helps you to write better python .  This book also throws light&lt;br&gt;
at the modern python features and best practices.&lt;/p&gt;

&lt;p&gt;Recommended for everyone who wants to write more maintainable code and survive&lt;br&gt;
the software onslaught .&lt;/p&gt;

&lt;p&gt;Thats it .. there are a lot of other good books also but I personally think these are the&lt;br&gt;
best in the business . comment down which books you are going to read and&lt;br&gt;
which have helped you in past . :)&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>python</category>
    </item>
    <item>
      <title>How and when to use __slots__ in python</title>
      <dc:creator>aabidsofi19</dc:creator>
      <pubDate>Thu, 05 May 2022 05:33:18 +0000</pubDate>
      <link>https://forem.com/aabidsofi/how-and-when-to-use-slots-in-python-153b</link>
      <guid>https://forem.com/aabidsofi/how-and-when-to-use-slots-in-python-153b</guid>
      <description>&lt;p&gt;Each python object has a _&lt;em&gt;dict&lt;/em&gt;_ attribute which is a dictionary containing&lt;br&gt;
all other attributes. E.g. when you type self.attr python is actually doing&lt;br&gt;
self.&lt;strong&gt;dict&lt;/strong&gt;['attr'].&lt;/p&gt;

&lt;p&gt;As you can imagine using a dictionary to store attribute takes some extra space&lt;br&gt;
&amp;amp; time for accessing it.&lt;/p&gt;

&lt;p&gt;What if you already knew which attributes your class is going to have . Then&lt;br&gt;
using _&lt;em&gt;dict&lt;/em&gt;_ to store attributes doesn't  seem to be a good idea as we don't&lt;br&gt;
need the dynamic nature of dicts .&lt;/p&gt;

&lt;p&gt;This is where slots come to rescue .&lt;/p&gt;
&lt;h1&gt;
  
  
  What are &lt;strong&gt;slots&lt;/strong&gt; in python
&lt;/h1&gt;

&lt;p&gt;_&lt;em&gt;slots&lt;/em&gt;_ is a class variable which allows us declare  the  attributes  of a&lt;br&gt;
class explicitly and denying the creation of _&lt;em&gt;dict&lt;/em&gt;_ and&lt;br&gt;
_&lt;em&gt;weakref&lt;/em&gt;_ for object instances .&lt;/p&gt;

&lt;p&gt;The expected attributes can be assigned to _&lt;em&gt;slots&lt;/em&gt;_  as a string, iterable,&lt;br&gt;
or sequence of strings with variable names used by instance .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space saved by  not having a _&lt;em&gt;dict&lt;/em&gt;_  can be significant  . Slots also&lt;br&gt;
improve the attribute lookup speed .&lt;/strong&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Usage of &lt;strong&gt;slots&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;Python utilises a considerably more compact internal representation for&lt;br&gt;
instances when you define _&lt;em&gt;slots&lt;/em&gt;_. Instead of a dictionary, each instance&lt;br&gt;
is created around a small fixed-sized array, similar to a tuple or list.&lt;/p&gt;

&lt;p&gt;As a result of using slots, you can &lt;strong&gt;no longer add new attributes to&lt;br&gt;
instances&lt;/strong&gt;;&lt;br&gt;
you are limited to the attribute names given in the _&lt;em&gt;slots&lt;/em&gt;_ specifier.( You&lt;br&gt;
can get around this by adding _&lt;em&gt;dict&lt;/em&gt;_ to the _&lt;em&gt;slot&lt;/em&gt;_ . The dict will only&lt;br&gt;
initialized when a dynamic attribute is added ) .&lt;/p&gt;

&lt;p&gt;Example showing usage of slots :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# A email class with out using slots
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;


&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailWithSlots&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;

  &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'subject'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'to'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'message'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;
    &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;


&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;EmailWithSlots&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'test'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'me@gmail.com'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'testing slots'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; test
&lt;/span&gt;
&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;  &lt;span class="c1"&gt;# cant access __dict__ because its not created
&lt;/span&gt;

&lt;span class="c1"&gt;# AttributeError                            Traceback (most recent call last)
# &amp;lt;ipython-input-40-b95667a7fb92&amp;gt; in &amp;lt;module&amp;gt;
#  ----&amp;gt; 1 email.__dict__
#
#AttributeError: 'EmailWithSlots' object has no attribute '__dict__'
&lt;/span&gt;
&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"aabid@gmail.com"&lt;/span&gt; &lt;span class="c1"&gt;# cant add an atribute that not in __slots__
&lt;/span&gt;
&lt;span class="c1"&gt;# ---------------------------------------------------------------------------
# AttributeError                            Traceback (most recent call last)
# &amp;lt;ipython-input-42-87651e4df821&amp;gt; in &amp;lt;module&amp;gt;
# ----&amp;gt; email.from = "aabid@gmail.com"
#
#AttributeError: 'EmailWithSlots' object has no attribute 'from'
&lt;/span&gt;


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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Pros And Cons  of &lt;strong&gt;slots&lt;/strong&gt;
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Pros
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;using _&lt;em&gt;slots&lt;/em&gt;_ has faster attribute access .&lt;/li&gt;
&lt;li&gt;_&lt;em&gt;slots&lt;/em&gt;_  reduces memory usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Cons
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Fixed attributes ( You can get around this by adding _&lt;em&gt;dict&lt;/em&gt;_ to the
 _&lt;em&gt;slots&lt;/em&gt;_ . The dict will only initialized when a dynamic attribute is added )&lt;/li&gt;
&lt;li&gt; _&lt;em&gt;slots&lt;/em&gt;_ are implemented at the class level by creating descriptors
 for each variable name. As a result, class attributes cannot be used to set
 default values for instance variables defined by _&lt;em&gt;slots&lt;/em&gt;_; otherwise, the
 class attribute would overwrite the descriptor assignment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Inheritance with &lt;strong&gt;slots&lt;/strong&gt;  .
&lt;/h1&gt;

&lt;p&gt;Working with slots and Inheritance is a little tricky and need to be taken care&lt;br&gt;
of .  Here is How :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The _&lt;em&gt;slots&lt;/em&gt;_ defined on parent class are automatically accessible to the
child .
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'y'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'z'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


   &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Inherited&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;


   &lt;span class="n"&gt;Inherited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__slots__&lt;/span&gt;

   &lt;span class="c1"&gt;#&amp;gt;&amp;gt; ['x', 'y', 'z']
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;If the subclass doesn't specify slots ( empty or new ) . The subclass will
get _&lt;em&gt;dict&lt;/em&gt;_ and _&lt;em&gt;weakref&lt;/em&gt;_ attributes in addition to slots from parent
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;   &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BaseClass&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
       &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'y'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;'z'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;


   &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Inherited&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

   &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InheritedWithSlots&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;BaseClass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

   &lt;span class="n"&gt;Inherited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__slots__&lt;/span&gt;
   &lt;span class="c1"&gt;#&amp;gt;&amp;gt; ['x', 'y', 'z']
&lt;/span&gt;
   &lt;span class="n"&gt;Inherited&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;
   &lt;span class="c1"&gt;#&amp;gt;&amp;gt; {}
&lt;/span&gt;
   &lt;span class="n"&gt;InheritedWithSlots&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;__dict__&lt;/span&gt;
   &lt;span class="c1"&gt;# AttributeError
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Nonempty _&lt;em&gt;slots&lt;/em&gt;_ does not work for classes derived from “variable-length” built-in types such as int, bytes and tuple.&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# this works fine because we are not using any additional slots in subclass
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MyInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# This will panic because we are adding non-empty slots .
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NewInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#TypeError: nonempty __slots__ not supported for subtype of 'int'
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;

&lt;p&gt;You can also use multiple inheritance with slots . But only one parent can&lt;br&gt;
have non empty _&lt;em&gt;slots&lt;/em&gt;_. Otherwise a typeerror is raised .&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;#lets have three slotted base classes
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"x"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"y"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;bar&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="n"&gt;__slots__&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"a"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"b"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;baz&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
   &lt;span class="n"&gt;__slots__&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# this will raise TypeError as we are inheriting from two classes
# with nonempty slots
&lt;/span&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;foobar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;pass&lt;/span&gt;

&lt;span class="c1"&gt;#&amp;gt;&amp;gt;TypeError: multiple bases have instance lay-out conflict
&lt;/span&gt;
&lt;span class="c1"&gt;# This shall work as only one of the inherited class has nonempty slots
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FooBaz&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;pass&lt;/span&gt;

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




&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Conclusion :
&lt;/h1&gt;

&lt;p&gt;_&lt;em&gt;slots&lt;/em&gt;_ allow us to explicitly state which instance variables to expect in&lt;br&gt;
the object&lt;/p&gt;

&lt;p&gt;Which gives us following benefits&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Faster Attribute Access&lt;/li&gt;
&lt;li&gt;Saves Memory&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A typical use case
&lt;/h2&gt;

&lt;p&gt;For classes that primarily serve as simple data structures, you can often greatly reduce&lt;br&gt;
the memory footprint of instances by adding the _&lt;em&gt;slots&lt;/em&gt;_ attribute to the&lt;br&gt;
class definition.&lt;/p&gt;

&lt;h2&gt;
  
  
  When &lt;strong&gt;slots&lt;/strong&gt; is a bad idea .
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Avoid them when you want to perform _&lt;em&gt;class&lt;/em&gt;_ assignment with another class that doesn't have them (and you can't add them) unless the slot layouts are identical. (I am very interested in learning who is doing this and why.)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid them if you want to subclass variable length builtins like long, tuple, or str, and you want to add attributes to them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Avoid them if you insist on providing default values via class attributes for instance variables.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Thanks , for reading till end Happy coding ..&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Tips for Writing Fast and Idiomatic Python</title>
      <dc:creator>aabidsofi19</dc:creator>
      <pubDate>Sat, 23 Apr 2022 06:30:22 +0000</pubDate>
      <link>https://forem.com/aabidsofi/tips-for-writing-fast-and-idiomatic-python-1f2j</link>
      <guid>https://forem.com/aabidsofi/tips-for-writing-fast-and-idiomatic-python-1f2j</guid>
      <description>&lt;p&gt;&lt;strong&gt;Orignally posted at &lt;a href="https://www.aabidsofi.com/posts/tips-for-writing-fast-and-idiomatic-python/"&gt;https://www.aabidsofi.com/posts/tips-for-writing-fast-and-idiomatic-python/&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Python tricks" is a tough one, cuz the language is so clean.&lt;br&gt;
E.g., C makes an art of confusing pointers with arrays and strings, which leads&lt;br&gt;
to lotsa neat pointer tricks; APL mistakes everything for an array, leading to&lt;br&gt;
neat one-liners; and Perl confuses everything period, making each line a joyous adventure ;-)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Tim Peters&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Python is praised for its readability. This makes it a good first language, and&lt;br&gt;
a popular choice for scripting and prototyping.&lt;/p&gt;

&lt;p&gt;In this post, we will look at some techniques that will make your Python&lt;br&gt;
code more readable and idiomatic. I am no mere an expert in python , these tips&lt;br&gt;
have helped me a lot in writing good python and I hope these will help you also&lt;br&gt;
in the same . If you have any more ideas , we would love to discuss them in&lt;br&gt;
comment section below ..&lt;/p&gt;
&lt;h2&gt;
  
  
  Tip 1. When there is recurring setup and tear down you should use a Context Manager so that you can use with statement
&lt;/h2&gt;

&lt;p&gt;Whenever working with external resources like files and database which involves&lt;br&gt;
setup of resource , checking for any exceptions and then finally closing the&lt;br&gt;
resource (tear down ) , rather than using the naive the naive approach of use try&lt;br&gt;
, except and finally pattern  use &lt;strong&gt;Context Managers&lt;/strong&gt; .&lt;/p&gt;

&lt;p&gt;Most times when your are working databases , locks and files the standard&lt;br&gt;
library provides the context managers which can be used through the &lt;strong&gt;with&lt;/strong&gt;&lt;br&gt;
statement . Lets take the trivial example of reading from a file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# non pythonic way
&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nb"&gt;file&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"text.txt"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;lines&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readlines&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nb"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="nb"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
# pythonic way much concise, and less bug prone

with open('text.txt') as f :
    f.readlines()

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

&lt;/div&gt;



&lt;p&gt;As you can see  we no longer need to setup the file and close it . This way is&lt;br&gt;
not only short but also conveys the &lt;strong&gt;business logic properly&lt;/strong&gt;. We don't have&lt;br&gt;
to do the setup and tear down ourselves which makes the code &lt;strong&gt;less error prone&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Session class of the request module implements the &lt;strong&gt;enter&lt;/strong&gt; and &lt;strong&gt;exit&lt;/strong&gt;&lt;br&gt;
methods and can be used as a context manager when you need to preserve cookies&lt;br&gt;
between requests, want to make multiple requests to the same host or just want&lt;br&gt;
to keep TCP connection alive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;requests&lt;/span&gt;
  &lt;span class="k"&gt;with&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Session&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;sess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
     &lt;span class="n"&gt;sess&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 2. Stop adding strings manually  use str.join instead .
&lt;/h2&gt;

&lt;p&gt;String objects are immutable in python and any concatenation operation creates a&lt;br&gt;
new string, copies the old string character by character and then appends the&lt;br&gt;
new string to be concatenated.&lt;/p&gt;

&lt;p&gt;Lets suppose we have a list of names that we need to concatenate into a single string&lt;br&gt;
separated by commas.&lt;/p&gt;

&lt;p&gt;The naive approach for doing this is :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"aabid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"john"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"mathew"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;new_string&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;

   &lt;span class="n"&gt;new_string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;While this approach seems to be pretty straightforward . But as I told you above&lt;br&gt;
that strings being immutable in python the above algorithm has a quadratic&lt;br&gt;
runtime which will create problems if the length of names becomes large.&lt;/p&gt;

&lt;p&gt;But don't worry there is a better , easier , and more pythonic way to achieve&lt;br&gt;
this with a linear runtime and that is the &lt;strong&gt;str.join()&lt;/strong&gt; .&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;str.join() takes an iterable as an argument and returns a string which is a&lt;br&gt;
concatenation of all the string objects in the iterable.&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;names&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"aabid"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"john"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s"&gt;"mathew"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;new_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;","&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;names&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 3.  Take advantage of magic methods ( dunder methods ) to your best
&lt;/h2&gt;

&lt;p&gt;If you have used Classes in python . You would have also used  the&lt;br&gt;
&lt;strong&gt;&lt;strong&gt;init&lt;/strong&gt;()&lt;/strong&gt;  method  to initialize the class state which is a dunder or magic&lt;br&gt;
method . magic methods have a double underscore as prefix as suffix thats why&lt;br&gt;
they are also called dunder methods&lt;/p&gt;

&lt;p&gt;Python uses the word "magic methods", because those methods really performs magic for you program. One of the biggest advantages of using Python's magic methods is that they provide a simple way to make objects behave like built-in types. That means you can avoid ugly, counter-intuitive, and nonstandard ways of performing basic operators.&lt;/p&gt;

&lt;p&gt;Lets suppose we have a class Stack ( which implements the Stack LIFO&lt;br&gt;
datastructure ) and we want ours know the number of items in the stack or&lt;br&gt;
length . If you are coming from java or any other language . I might think&lt;br&gt;
of adding getLength() method  to achieve the same .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# basic  implementation
&lt;/span&gt;    &lt;span class="p"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getLength&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;pass&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;The implementation is quite simple but our users need to remember the&lt;br&gt;
getLength function .&lt;/p&gt;

&lt;p&gt;There is a more pythonic way of getting the length of sequences and you know&lt;br&gt;
its &lt;strong&gt;len()&lt;/strong&gt; . To implement the  len() method for our class we will use the&lt;br&gt;
dunder &lt;strong&gt;len&lt;/strong&gt;() method . remember that dunder methods should not be called&lt;br&gt;
directly from code but the interpreter calls them underneath . in this the&lt;br&gt;
builtin len() .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stack&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;

    &lt;span class="c1"&gt;# basic implementation
&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__len__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Stack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# now we can use len to check the no of items in Stack foo
&lt;/span&gt;
&lt;span class="n"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 4.  Don't Create getters and setters  use  @property decorator
&lt;/h2&gt;

&lt;p&gt;Suppose you have  a class Book which has  fields author and name. You want the&lt;br&gt;
field  to be read only so that your users will not change the author of a book .&lt;/p&gt;

&lt;p&gt;One way to do this by storing the author in a private variable and defining a&lt;br&gt;
method getAuthor() to retrieve it .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# non pythonic
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;getAuthor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt;


&lt;span class="n"&gt;book1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clean Code"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Uncle Bob"&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;getAuthor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt; Uncle Bob
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While the above works completely fine . But  there is a better way , using the&lt;br&gt;
@property decorator.&lt;/p&gt;

&lt;p&gt;Python  provides us with a built-in @property decorator which makes usage of&lt;br&gt;
getter and setters much easier in Object-Oriented Programming.&lt;/p&gt;

&lt;p&gt;By default methods decorated with @property are read only can be accessed with&lt;br&gt;
using the &lt;strong&gt;object.property&lt;/strong&gt; notation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# pythonic way using @property decorators
&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt;


&lt;span class="n"&gt;book1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clean Code"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Uncle Bob"&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt; Uncle Bob
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This doesn't end here , what if the requirements change in future and your are&lt;br&gt;
allowed to mutate the author only if its  the name starts with a "S" ( sounds&lt;br&gt;
silly but makes a good example )&lt;/p&gt;

&lt;p&gt;The non pythonic approach would be to create a  setAuthor() method and validate&lt;br&gt;
the input in it. But there is a better approach in python and thats the&lt;br&gt;
&lt;strong&gt;property.setter&lt;/strong&gt; decorator . which allows you to mutate a property .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Book&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;author&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="nb"&gt;property&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;__author&lt;/span&gt;

    &lt;span class="o"&gt;@&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setter&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;author&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="c1"&gt;# business logic
&lt;/span&gt;        &lt;span class="p"&gt;....&lt;/span&gt;

&lt;span class="n"&gt;book1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Book&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Clean Code"&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Sebastian"&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt;

&lt;span class="c1"&gt;# &amp;gt;&amp;gt; Sebastian
&lt;/span&gt;
&lt;span class="n"&gt;book&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;author&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Seb&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 5.  Use &lt;strong&gt;repr&lt;/strong&gt; for beautiful debugging experience
&lt;/h2&gt;

&lt;p&gt;According to the official documentation,  &lt;strong&gt;repr&lt;/strong&gt; is used to compute the&lt;br&gt;
“official” string representation of an object and is typically used for&lt;br&gt;
debugging.&lt;/p&gt;

&lt;p&gt;Lets create a person class and define a &lt;strong&gt;repr&lt;/strong&gt; method for it which will return&lt;br&gt;
a string representation for the object .Ideally, the representation should be&lt;br&gt;
information-rich and could be used to recreate an object with the same value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__init__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;
        &lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;__repr__&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
        &lt;span class="n"&gt;rep&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sa"&gt;f&lt;/span&gt;&lt;span class="s"&gt;'Person( "&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;" , &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;  )'&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;rep&lt;/span&gt;


&lt;span class="c1"&gt;# Let's make a Person object and print the results of repr()
&lt;/span&gt;
&lt;span class="n"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;#&amp;gt;&amp;gt;  Person("John", 20 )
&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 6.  Use Custom Exceptions properly
&lt;/h2&gt;

&lt;p&gt;Creating custom exception in python is pretty easy . But that doesn't mean you&lt;br&gt;
should be creating them everywhere.&lt;/p&gt;

&lt;p&gt;Only Create a custom exception when you have a complex error . Like&lt;br&gt;
suppose you have a cart object which a  checkout method. You don't want the&lt;br&gt;
checkout to work if the cart is empty so ,  creating a Exception CartEmpty makes&lt;br&gt;
a sense here .&lt;/p&gt;

&lt;p&gt;Don't suffix the exception name with Exception . I have seen a lot of people (&lt;br&gt;
including me ) naming their exceptions like &lt;strong&gt;&lt;em&gt;CartEmptyException&lt;/em&gt;&lt;/strong&gt; . This&lt;br&gt;
doesn't make any sense . Keep it simple &lt;strong&gt;CartEmpty&lt;/strong&gt;  .&lt;/p&gt;

&lt;p&gt;You don't need a custom exception every time . There are about 137 exceptions in&lt;br&gt;
python . Take advantage of these .&lt;/p&gt;
&lt;h2&gt;
  
  
  Tip 7.  Don't fear using built ins
&lt;/h2&gt;

&lt;p&gt;Python is a powerful programming language with many built-in features that allow you to do more with less code. For example, the range() function allows you to create a list of numbers easily, and the len() function returns the length of a string or list.&lt;/p&gt;

&lt;p&gt;In addition, Python has a number of libraries that you can use to simplify tasks such as data analysis and machine learning. The SciPy library, for example, provides modules for linear algebra, optimization, integration, and more.&lt;/p&gt;

&lt;p&gt;Python also has a rich collection of complex datastructures like heap , priory&lt;br&gt;
queues ,  dequeue , queue , orderedDict , and many more .  Rather than reinventing&lt;br&gt;
the wheel try to take advantage of these . These are way more optimized and fast&lt;br&gt;
than you would create yourself&lt;/p&gt;
&lt;h2&gt;
  
  
  Tip 8.  Use  Comprehensions in place of loops  .
&lt;/h2&gt;

&lt;p&gt;List comprehensions are a Pythonic way to create lists. They are more concise&lt;br&gt;
and readable than for loops, which is why they should be used in place of native&lt;br&gt;
for loops.&lt;/p&gt;

&lt;p&gt;The syntax for list comprehensions is:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;[ expression for item in sequence if condition ]&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is an example of a list comprehension that creates the list of even numbers between 0 and 10:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
  &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;==&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tip 9.  Use multiple variable assignment any unpacking
&lt;/h2&gt;

&lt;p&gt;Pythons multiple assignment feature is a nice perk that most programming languages lack. In its simplest form, it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"something"&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This is nice because it shortens and simplifies code. However, I rarely get to use it. A more practical option is unpacking iterables into multiple variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;some_list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"value1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"value2"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;some_list&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a better option than assigning values to each variable using indices&lt;/p&gt;

&lt;p&gt;Another important use-case of multiple variable assignment is swapping variables&lt;br&gt;
without using a temporary variable .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# swapping a variable using a temporary  variable
&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="n"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;
&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;temp&lt;/span&gt;

&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt; 20
&lt;/span&gt;
&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# &amp;gt;&amp;gt;10
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;
&lt;span class="c1"&gt;# swapping variables using multiple assignment
&lt;/span&gt;
&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;This solution is way more concise and easy to understand&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for  reading till end . I would love to hear about the tips and hacks&lt;br&gt;
you use to improve your code .&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>tips</category>
    </item>
    <item>
      <title>Best Opensource HTML / Vue  Template for Developer Portfolio</title>
      <dc:creator>aabidsofi19</dc:creator>
      <pubDate>Fri, 01 Oct 2021 08:32:58 +0000</pubDate>
      <link>https://forem.com/aabidsofi/best-opensource-html-vue-template-for-developer-portfolio-3ioo</link>
      <guid>https://forem.com/aabidsofi/best-opensource-html-vue-template-for-developer-portfolio-3ioo</guid>
      <description>&lt;p&gt;Minimal and Modern theme for Portfolios . Created using vuejs and vuetify . The template is completely free , opensource .&lt;/p&gt;

&lt;p&gt;Give it a star 🌟 on Github if you find it useful.&lt;/p&gt;

&lt;p&gt;GitHub Repo &lt;a href="https://github.com/aabidsofi19/neva-portfolio-template"&gt;https://github.com/aabidsofi19/neva-portfolio-template&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Check out the live demo &lt;a href="https://neva-portfolio-vue.netlify.app"&gt;https://neva-portfolio-vue.netlify.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Link to docs &lt;a href="https://www.aabidsofi.com/docs/neva-portfolio-vue/"&gt;https://www.aabidsofi.com/docs/neva-portfolio-vue/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The template is best fit for developers and designers wanting to showcase their portfolio to  the mighty web audience &lt;/p&gt;

&lt;p&gt;Fully Responsive on Mobile Devices&lt;/p&gt;

&lt;p&gt;The template has been crafted with love using Vuejs, Vuex, and Vuetify.&lt;br&gt;
The template follows a component-based design so, is easy to customize for both developers and people not familiar with code.&lt;/p&gt;

&lt;p&gt;The template has been specially crafted to be simple to customize for people not familiar with code.&lt;/p&gt;

&lt;p&gt;The installation process is pretty simple and won’t take more than 10 minutes. So let’s begin.&lt;/p&gt;

&lt;p&gt;Hav’nt installed the template yet,clone the repo now.&lt;/p&gt;

&lt;p&gt;The template has been licensed under the MIT license. Which means you are not bound for any type of credit. But surely giving one in your footer will be helpful and appreciated a lot.&lt;/p&gt;

&lt;p&gt;Star the repo on Github and follow me. It means a lot ❤️.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>opensource</category>
      <category>portfolio</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
