Functional Programming in Python


Functional programming languages are specially designed to handle symbolic computation and list processing applications. Functional programming is based on mathematical functions. Some of the popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc.

Characteristics of Functional Programming

The most prominent characteristics of functional programming are as follows −

  • Functional programming languages are designed on the concept of mathematical functions that use conditional expressions and recursion to perform computation.

  • Functional programming supports higher-order functions and lazy evaluation features.

  • Like OOP, functional programming languages support popular concepts such as Abstraction, Encapsulation, Inheritance, and Polymorphism.

Advantages of Functional Programming

Here are the advantages −

Modularity − It forces you to break apart your problem into small pieces. Programs are more modular as a result. It’s easier to specify and write a small function that does one thing than a large function that performs a complicated transformation. Small functions are also easier to read and to check for errors.

Debugging is simplified

The functions are generally small and clearly specified, therefore debugging is simplified. When a program doesn’t work, each function is an interface point where you can check that the data are correct.

Ease of Testing

Testing is easier since each function is a possible subject for a unit test. Functions don’t depend on system state that needs to be replicated before running a test, instead you only have to synthesize the right input and then check that the output matches expectations.

Composability

While working on a functional-style program, you’ll write a number of functions with varying inputs and outputs. Some of these functions will be unavoidably specialized to a particular application, but others will be useful in a wide variety of programs.

Functions are first-class objects

A function should have the following for supporting functional style programming, Python has both: Taking another function as an argument and returning another function to its caller.

In Python, the functions are considered as first-class objects i.e. we can store the functions in a variable, return function from a function, etc.

Below are some examples displaying functions in Python useful for understanding Decorators.

Functions as objects

The functions are considered as object in this example. Here, the function demo() is assigned to a variable 

Example

# Creating a function def demo(mystr): return mystr.swapcase() # swapping the case print(demo('Thisisit!')) sample = demo print(sample('Hello'))

Output

tHISISIT!
hELLO

Pass the function as an argument

In this the function is passed as an argument. The demo3() function calls demo() and demo2() function as a parameter.

Example

def demo(text): return text.swapcase() def demo2(text): return text.capitalize() def demo3(func): res = func("This is it!") # Function passed as an argument print (res) # Calling demo3(demo) demo3(demo2)

Output

tHIS IS IT!
This is it!

Updated on: 16-Sep-2022

558 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements