Jul 20, 2020

Functions in Python | PYTHON Language | Coding Winds

Python Functions

Hello guys, we will in this blog learn about Python functions.  A function is statements written in that works only when called.

A function in Python is defined with the def keyword. Functions do not have declared return types. A function without an explicit return statement returns None. In the case of no arguments and no return value, the definition is very simple.

Calling the function is performed by using the call operator () after the name of the function.


1.png

Arguments

The arguments of a function are defined within the def statement. Like all other variables in Python, there is no explicit type associated with the function arguments.

Function arguments can optionally be defined with a default value. The default value will be assigned in the case that the argument is not present in the call to the function. All arguments without default values must be listed before arguments with default values in the function definition.

2.png

Default value of score if not mention while calling the function will be zero.

What if we change the position of arguments, will it work the same?

3.png

Check the output. Doubt cleared?

Note: - Look carefully at the example above. There is an asymmetry in the use of the = sign for defining vs. passing arguments that can be confusing to beginners. An argument with a default value can be passed using only position and an argument without a default can be passed using a keyword.

Mutable Arguments and Binding of Default Values

When defining default arguments, take special care with mutable data types. The instance of the default value is bound at the time the function is defined. Consequently, there is a single instance of the mutable object that will be used across all calls to the function.

4.png

As a result of the above its is useful to use default value as None. 

5.png

Error in the code. Instead of baseitem=[], it should be baseitem=None.

Accepting Variable Arguments

Functions can have two special arguments. 

The first is a variable-length, named tuple of any additional positional arguments received by the function. This special argument is identified by prefixing it with *. 

The second is a variable-length dictionary containing all keyword arguments passed to the function that were not explicitly defined as part of the function arguments. This is identified by **.

It is not required, but for easiness, to name these two arguments args and kwargs.

6.png

Use the function, and note the output.

Watch out sometimes complex functioning gives rise to errors.

Unpacking Argument Lists

It is also possible to construct argument lists (positional or keyword) and pass them into a function.

For positional arguments, insert them into a tuple / list and prepend with an asterisk (*) in the function call.

For keyword arguments, use a dictionary and prepend with two asterisks (**).

7.png

Note: The example above shows another potentially confusing asymmetry in Python. You can pass arguments using the regular style to a function defined using variable arguments, and you can pass unpacked variable argument lists to a function defined without variable arguments.

Scope

Each function evaluation creates a local namespace that is manipulated at any level within the function.

So, variables can be initially defined at a seemingly lower level of scope than they are eventually used.

8.png


Note: This model for scope can simplify your code, but pay attention. If you don’t anticipate all code paths, you can end up referencing undefined variables.

No comments:

Post a Comment