Home >Backend Development >Python Tutorial >Functional programming in Python
Functional programming languages are specifically designed to handle symbolic computation and list processing applications. Functional programming is based on mathematical functions. Some popular functional programming languages include: Lisp, Python, Erlang, Haskell, Clojure, etc.
The most significant features of functional programming are as follows:
Functional programming languages are designed based on the concept of mathematical functions, which use conditional expressions and recursion to perform calculations.
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.
The following are the advantages -
Modularity - It forces you to break the problem into small pieces. Programs are more modular as a result. Writing a small function that does just one thing is easier to specify and write than writing a large function Perform complex transformations. Small functions are also easier to read and inspect mistake.
These functions are usually small and well-defined, so debugging is simplified. When the program is not working properly, each function is an interface point where you can check that the data is correct.
Testing is easier because every function is a possible subject of unit testing. Functions do not rely on system state that needs to be copied before running the test, instead you simply synthesize the correct inputs and then check that the output is as expected.
When writing functional programs, you will write many functions with different inputs and outputs. Some of these functions will inevitably be specialized for specific applications, but others will be very useful in a variety of programs.
To support functional programming, a function should have the following conditions, and Python does both: take another function as an argument and return the other function to its caller.
In Python, functions are treated as first-class objects, i.e. we can store functions in variables, return functions from functions, etc.
The following are some examples of displaying functions in Python, which are very useful for understanding decorators.
In this example, functions are treated as objects. Here, the function demo() is assigned to the variable −
# Creating a function def demo(mystr): return mystr.swapcase() # swapping the case print(demo('Thisisit!')) sample = demo print(sample('Hello'))
tHISISIT! hELLO
Passed as a parameter in this function. The demo3() function calls the demo() and demo2() functions as parameters.
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)
tHIS IS IT! This is it!
The above is the detailed content of Functional programming in Python. For more information, please follow other related articles on the PHP Chinese website!