Python Quick Notes :: Part - 6
Bhaskar S | 03/24/2013 |
Hands-on With Python - VI
Python supports the Functional Programming paradigm as well. Functional Programming has its roots from Mathematics where a computation involves evaluating one or more function(s), each of which take one or more input value(s) to produce some output value(s). The following are some of the characteristics of Functional Programming in Python:
Pure Function - A function that has no side-effect in that there is no change to the input value(s). In other words, a function does not alter the state of the input
First Class Object - A function that is treated like any other data type or object. They can be:
bound to an identifier
stored as a value in a data structure like a list
passed as argument(s) to a function
returned as value from a function
Higher Order Function - A function that can either accept other function(s) as input argument(s) or return a function as an output. The following are some of the built-in functions that demonstrate this concept:
filter(fn, items) - takes a specified function fn and a sequence items (list or tuple) and returns a new sequence (list or tuple) that contains only those items from the original sequence for which the function is True
map(fn, items) - takes a specified function fn and a sequence items (list or tuple) and returns a new sequence (list or tuple) of all the items after the given function is applied to each item of the given sequence
reduce(fn, items) - takes a specified function fn and a sequence items (list or tuple) and returns a single value by first applying the given function to the first two elements of the sequence, then applying the given function to the result of that computation and the third item in the sequence and so on
Lamda Expression - A function that is not bound to a name (anomymous function)
Closures - A function that closes over the environment in which it is defined. In other words, a function that remembers values bound to variables (that were in scope) when it was created
List Comprehension - Ability to create a new list from an existing list
The following is the python program named sample-29.py:
Execute the following command:
python sample-29.py
The following is the output:
-----> Concept of Pure Functions n (before) = 5 fact(5) = 120 n (after) = 5 -----> Concept of First Class Objects func_obj invoked with n = 5 -----> Concept of Higher Order Functions Higher order function hofn1 called with arg: 10 Invoked fn with arg: 10 Invoked hofn2 Invoked fn with arg: 15 List (before filter): [1, 2, 3, 4, 5, 6, 7, 8, 9] List (after filter): [3, 6, 9] List (after map): [2, 4, 6, 8, 10, 12, 14, 16, 18] Value (afetr reduce): 362880 -----> Concept of Lambdas List (before lambda filter): [1, 2, 3, 4, 5, 6, 7, 8, 9] List (after lambda filter): [2, 4, 6, 8] List (after lambda map): [1, 8, 27, 64, 125, 216, 343, 512, 729] Value (afetr lambda reduce): 45 -----> Concept of Closures Invoke div_by_2(24) = 12 -----> Concept of List Comprehension lst4 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] lst5 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11] lst6 = [2, 4, 6, 8, 10] lst9 = [('A', 1), ('A', 2), ('B', 1), ('B', 2)]
The following are some of the miscellaneous concepts in Python:
*args - syntax used to pass variable number of positional arguments to a function
**kwargs - syntax used to pass variable number of keyword based arguments to a function
__dict__ - pre-defined dictionary attribute created by Python in any user-defined class to store an objects attributes and their corresponding values
The following is the python program named sample-30.py:
Execute the following command:
python sample-30.py
The following is the output:
Invoked myfunc1() with args: 1 2 3 Invoked myfunc1() with args: A B C D E Invoked myfunc2() with keywork args: type = LANG name = PYTHON value = 3 Invoked myfunc2() with keywork args: a = 1 c = 3 b = 2 e = 5 d = 4 obj1.get_name() = Python obj1.__dict__ = {'price': 9.99, 'name': 'Python'}
References