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.