<?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: Vruttant Balde</title>
    <description>The latest articles on DEV Community by Vruttant Balde (@vruttant).</description>
    <link>https://dev.to/vruttant</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%2F163880%2F76928c44-df1c-4d52-bf61-0980b180ddff.png</url>
      <title>DEV Community: Vruttant Balde</title>
      <link>https://dev.to/vruttant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vruttant"/>
    <language>en</language>
    <item>
      <title>Passing Mutables as Function Parameters in Python</title>
      <dc:creator>Vruttant Balde</dc:creator>
      <pubDate>Thu, 18 Aug 2022 09:29:01 +0000</pubDate>
      <link>https://dev.to/vruttant/passing-mutables-as-function-parameters-in-python-1gck</link>
      <guid>https://dev.to/vruttant/passing-mutables-as-function-parameters-in-python-1gck</guid>
      <description>&lt;p&gt;In Python it is comparatively very easy to create functions, meaning there are no return types or I should say no types at all!&lt;/p&gt;

&lt;p&gt;Okay, So some of you might say that no type hinting is still a thing but I'm going to deny the fact and say that..&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9dQhod40--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyj1ridvt4ben37yucv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9dQhod40--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyj1ridvt4ben37yucv4.png" alt="A funny meme on python type hints" width="880" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;They do certainly help for writing clean and understandable code.&lt;/p&gt;

&lt;p&gt;Anyway coming on the topic in hand, Have you ever tried passing a mutable object to a function as a parameter and manipulating it afterwards? No? Because you cannot at least as a positional argument. It won't just run and it doesn't make much sense either.&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;def&lt;/span&gt; &lt;span class="nf"&gt;pos_func&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;Ok so how do we access the passed list now? You get it.&lt;br&gt;
Now, if we passed a list as a keyword argument then accessing it is not a big deal, But manipulating it 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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;keyword_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt;
    &lt;span class="n"&gt;array&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;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;array&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;keyword_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&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;keyword_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&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;keyword_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You would expect the output will be the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1]
[2]
[3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But no, the actual output is&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1]
[1, 2]
[1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason is&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The default value is evaluated only once. This makes a difference when the default is a mutable object such as a list, dictionary, or instances of most classes.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you don't want this behaviour and don't want the default to be shared between subsequent calls, you can write the function like this instead:&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;def&lt;/span&gt; &lt;span class="nf"&gt;keyword_func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;num&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;array&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;array&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;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt; 
    &lt;span class="n"&gt;array&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;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's how you pass a mutable object to a function if you don't want the object to be shared between every call.&lt;/p&gt;

&lt;p&gt;This was my first blog here at dev.to. Feel free to comment anything you feel about this blog. Next blog would be related to positional arguments, keyword arguments, using them together and how they are separated. Thank you!&lt;/p&gt;

&lt;p&gt;Credits: &lt;br&gt;
&lt;a href="https://docs.python.org/3/library/typing.html"&gt;Type hinting Quote&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.reddit.com/r/ProgrammerHumor/comments/co635o/just_use_listdictstr_any_bro_its_more_pythonic/"&gt;Type hinting Meme&lt;/a&gt;&lt;br&gt;
&lt;a href="https://docs.python.org/3/tutorial/controlflow.html#more-on-defining-functions"&gt;Reference and Examples&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
