Avoid these Gotchas/Errors related to parameters and arguments in Python
Tags: python, programming, codenewbie, computerscience
Published: 2021-05-02 06:05:11

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3>We will discuss a couple of Errors and One Gotcha related to Python arguments and parameters.&nbsp;</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Before moving on, let’s discuss a few terms. Check out the code snippet below. It’s a simple function that prints the sum of its parameters.</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*RNLzpLb7LkTmr0XqQgmD9A.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:list -->
<ul><li><strong>Parameters</strong>- They are the names listed in the function’s definition. Eg: In the above code snippet, <strong>num1</strong> and <strong>num2</strong> are parameters</li><li><strong>Arguments</strong>- They are the values we pass while calling the function. Eg: In the above code snippet, in both calls to the function <strong>10</strong> and <strong>12</strong> are arguments passed to the function</li><li><strong>Positional Argument</strong>- When we pass arguments to a function without explicitly assigning them to parameters. Eg: In the above code snippet, we use positional arguments in the first call to <strong>func</strong></li><li><strong>Keyword Argument</strong>- When we pass arguments to a function by explicitly assigning them to parameters. Eg: In the above code snippet, we use keyword arguments in the second call to <strong>func</strong></li><li><strong>Default Values</strong>- The value assigned to the parameter during the function definition. Eg: In the above code snippet, <strong>20</strong> is a default value assigned to the parameter <strong>num2</strong></li></ul>
<!-- /wp:list -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3>Order of default parameters</h3>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>RULE: If you have a parameter with a default value when defining your function, all the parameters following it MUST also have default&nbsp;values</p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":4} -->
<h4>Incorrect</h4>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*wdUYo9lnc2l84fMxTTcNPw.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>In the above function definition for <strong>func</strong>, a couple of parameters (<strong>num3, num4</strong> )with no default value follow a parameter (<strong>num2</strong> )with a default value. This results in a Syntax Error.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Correct</h4>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*f_d3g1PzmBIQYWqXDL3g3w.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This doesn’t result in any error since we have specified default values for the variables <strong>num3</strong> and <strong>num4</strong></p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3>Order of keyword&nbsp;argument</h3>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>RULE: When calling a function, a keyword argument must follow a keyword argument. Therefore, No positional arguments can follow a keyword argument.</p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":4} -->
<h4>Incorrect</h4>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*tvlooDuaEbdhjmj2Cd4Xbw.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>12</strong> is passed as a keyword argument. However, the arguments following it (<strong>20</strong> and <strong>30</strong>) are passed as positional arguments. This results in a Syntax Error.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Correct</h4>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*ua-iMnaLkQQSIDt-jxpnuQ.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>This time, we pass the arguments (<strong>20</strong> and <strong>30</strong>) as keyword arguments. Therefore, no error is raised.</p>
<!-- /wp:paragraph -->

<!-- wp:separator -->
<hr class="wp-block-separator"/>
<!-- /wp:separator -->

<!-- wp:heading {"level":3} -->
<h3>Value of Default Parameters</h3>
<!-- /wp:heading -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>MISCONCEPTION: Default Values are assigned to parameters during each function&nbsp;call</p></blockquote>
<!-- /wp:quote -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p>Correct: The default value is only assigned once. In the future functions calls the previous value is&nbsp;used</p></blockquote>
<!-- /wp:quote -->

<!-- wp:paragraph -->
<p>This will make more sense when you follow the example below</p>
<!-- /wp:paragraph -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*ZNWUIzjRO5lN4Y4NPwEU7w.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The function has two parameters <strong>num</strong> and <strong>arr</strong>. <strong>arr</strong> has a default value of an empty list. The function doesn't do anything fancy. It simply inserts the parameter <strong>num</strong> into the array <strong>arr</strong> and then prints the array.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Expectation</h4>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">'''
OUTPUT
[10]
[20]
[30]
'''</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>So basically each time we call the function, <strong>arr</strong> is set to the default value of an empty list, and <strong>num</strong> is inserted into it.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><strong>But this is NOT the correct output</strong></p></blockquote>
<!-- /wp:quote -->

<!-- wp:heading {"level":4} -->
<h4>Actual</h4>
<!-- /wp:heading -->

<!-- wp:preformatted -->
<pre class="wp-block-preformatted">'''
OUTPUT
[10]
[10, 20]
[10, 20, 30]
'''</pre>
<!-- /wp:preformatted -->

<!-- wp:paragraph -->
<p>Huh, weird right?🤯</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Basically, the default value is set during the first function call. During the following function calls, the default value is not assigned again. Instead, the previous value is used.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":4} -->
<h4>Solution</h4>
<!-- /wp:heading -->

<!-- wp:image -->
<figure class="wp-block-image"><img src="https://cdn-images-1.medium.com/max/1600/1*hMmbawVnSoAZsqof7CPXYA.png" alt=""/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Instead of assigning the empty list as a default parameter, assign <strong>None</strong> as a default value and assign the empty list inside the function.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Conclusion</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>I hope you learned something new today. Parameters and Arguments seem like easy concepts but in reality, they are slightly more complex than you think. Now you know the errors and misconceptions to avoid.</p>
<!-- /wp:paragraph -->

<!-- wp:quote -->
<blockquote class="wp-block-quote"><p><em>Connect with me on </em><a href="https://www.linkedin.com/in/rahulbanerjee2699/" rel="noreferrer noopener" target="_blank"><em>LinkedIn</em></a><em>, </em><a href="https://twitter.com/rahulbanerjee99" rel="noreferrer noopener" target="_blank"><em>Twitter</em></a></p></blockquote>
<!-- /wp:quote -->