Monday, April 26, 2021

The way of function interacts with its arguments

 Consider the below example.

Example - 1







The output is given below.





Here, changing the parameter's value doesn't propagate outside the function.

This also means that a function receives the argument's value, not the argument itself. This is true for scalars.


Let's apply this theory for lists.

Example - 2







The output is,





Example - 3







The output is,




Explanation:-

  1. If the argument is a list, then changing the value of the corresponding parameter doesn't affect the list (But variables containing lists are stored in a different way than scalars)
  2. But if we change a list identified by the parameter (not the parameter), the list will reflect the change.

Functions & Scopes - the "global" keyword

There's a special Python method which can extend a variable's scope in a way which includes the functions' bodies even if we want not only to read the values, but also to modify them.

This effect is caused by a keyword named "global".

Using this keyword inside a function with the name or names separated with commas of a variable(s), forces Python to refrain from creating a new variable inside the function - the one accessible from outside will be used instead.

In other words, this name becomes global. It has a global scope and it doesn't matter whether it's the subject of read or assign.


Look at the example given below.



Here the output is,


Saturday, July 4, 2020

Functions & Scopes


The scope of a name (e.g., a variable name) is the part of a code where the name is properly recognizable.

For example, the scope of a function's parameter is the function itself. The parameter is inaccessible outside the function.

Let’s consider the below snippet.








Here, we get the following error message.





So let’s consider the below snippet.










Output is







A variable existing outside a function has a scope inside the functions’ bodies, excluding those of them which define a variable of the same name.


It also means that the scope of a variable existing outside a function is supported only when getting its value (reading). Assigning a value forces the creation of the function's own variable.

Lists & Functions


A list can also be sent to a function as an argument. So if we pass a list to a function, the function has to handle it like a list. 
Consider the below snippet.













Output is 12.


But if we invoke to the function in below way:






Python will response in below way.






This is caused by the fact that a single integer value mustn't be iterated through by the “for” loop.


A list can also be a result of a function. It mean, any entity recognizable by Python can be a function result.

Consider the below snippet.











Output is



"None" keyword


Its data doesn't represent any reasonable value - actually, it's not a value at all; hence, it mustn't take part in any expressions.

“None” is a keyword.

Let’s consider the below snippet & output.








This cause a runtime error, described by the above message.

There are only two kinds of circumstances when “None” can be safely used:
  • when you assign it to a variable (or return it as a function's result)
  • when you compare it with a variable to diagnose its internal state.
Like below.







If a function doesn't return a certain value using a return expression clause, it is assumed that it implicitly returns “None”.

Sometimes it may be the symptom of a subtle mistake inside the function.








Output is

The “return” instruction


This is a keyword in Python. This instruction has two different variants. Let’s consider them separately.

“return” without an expression

When it used inside a function, it causes the immediate termination of the function’s execution, and an instant return (hence the name) to the point of invocation.

If a function is not intended to produce a result, using the “return” instruction is not obligatory. It will be executed implicitly at the end of the function.

Let’s consider the below example.













Output will be







And the next example is












Output will be








“return” with an expression

The second “return” variant is extended with an expression.

function () :
   return expression

So, here consider the below example.










Result is





Sometimes that function not return the value and ignore it.











Output is


Parameterized Functions


It happens at times that a particular parameter's values are in use more often than others. Such arguments may have their default (predefined) values taken into consideration when their corresponding arguments have been omitted.

Let’s consider the below example.









Output will be,







Both parameters have their default values now, look at the code below.







Output will be


Mixing Positional & Keyword Arguments


We can mix both fashions if we want - there is only one unbreakable rule: we have to put positional arguments before keyword arguments.

Let’s consider below snippet.






We can get the output as





This is a pure example of positional argument passing. Also we can replace these arguments like below way.





So the output will be





Let’s try to mix both styles now.






Output will be





Let's analyze it:
  • The argument (3) for the “a” parameter is passed using the positional way.
  • The arguments for c” and “b” are specified as keyword ones.

But if we try to pass more than one value to one argument, all we'll get is a runtime error. Look at the snippet below.







This will get the following error.






Look at the snippet below. This code is fully correct, but it doesn’t make much sense.






Output is





Everything is correct. But leaving in just one keyword argument looks a bit weird.

Thursday, July 2, 2020

Keyword Argument Passing


Here the meaning of the argument is dictated by its name, not by its position.

Look at the below snippet.








Output is






The concept is clear - the values passed to the parameters are preceded by the target parameters' names, followed by the “=” sign.

The position doesn't matter here - each argument's value knows its destination on the basis of the name used.

Positional Parameter Passing


A technique which assigns the ith (first, second, and so on) argument to the ith (first, second, and so on) function parameter is called positional parameter passing, while arguments passed in this way are named positional arguments.

Let’s consider the below snippet.








Output is




Positional parameter passing is intuitively used by people in many social occasions. For example, it may be generally accepted that when we introduce ourselves we mention our first name(s) before our last name, e.g., "My name's John Doe".

Let's implement that social custom in Python.









Output is



Parameterized Functions


A parameter is actually a variable, but there are two important factors that make parameters different & special.

Parameters exist only inside functions in which they have been defined, and the only place where the parameter can be defined is a space between a pair of parentheses in the “def” statement.

Assigning a value to the parameter is done at the time of the function’s invocation, by specifying the corresponding argument.

We should remember this carefully. Specifying one or more parameters in a function’s definition is also a requirement, and we have to fulfill it during invocation. We must provide as many arguments as there are defined parameters.

Failure to do so will cause an error.

Let’s consider this example.








This code will produce the following output.





The value of the argument used during invocation (1) has been passed into the function, setting the initial value of the parameter named “number”.

Also it’s legal and possible to have a variable named the same as a function’s parameter. Let’s see the below snippet.










Output is






A situation like this activates a mechanism called shadowing.

Parameter “x” shadows any variable of the same name, but... only inside the function defining the parameter.

The parameter named “number” is a completely different entity from the variable named “number”.

A function can have as many parameters as we want, but the more parameters we have, the harder it is to memorize their roles and purposes.

Let's modify the function - it has two parameters now.










Output is