# Composition

Function composition refers to the way of combining multiple functions in such a manner that one function’s output becomes the input of the second function and so on. Furthermore, it enables the creation of complex types by combining objects of other types. Its representation can take place as F(G(x)) in case the functions are “F” and “G”, where “x” is the argument.

### Simple Demonstration of Composition

Below is a simple demonstration of composition in the Python programming language:

# Function to add 2

Furthermore, # to a number

return x + 2

Moreover, # Function to multiply

Also, # 2 to a number

def multiply(x):

return x * 2

# Printing the result of

Furthermore, # composition of add and

Moreover, # multiply to add 2 to a number

Also, # and then multiply by 2

Another important point- print("Adding 2 to 5 and multiplying the result with 2: ",

Finally, Output:

First of all, Adding 2 to 5 and multiplying the result with 2: 14

Moreover, First, the calling of the add() function takes place on input 5. Furthermore, the functions adds 2 to the input.

The input turns out to be 7, is given as the input to multiply() which undertakes multiplication of it by 2. Most noteworthy, the output turns out to be 14.

### Efficient Way of Implementing Composition

An efficient and better way to implement the composition of function is given below. As such, it is possible to create a special function that combines any two functions.

# Function to combine two

Furthermore, # function which it accepts

Moreover, # as argument

def composite_function(f, g):

return lambda x : f(g(x))

Also, # Function to add 2

return x + 2

# Function to multiply 2

def multiply(x):

return x * 2

Furthermore, # Composite function returns

Moreover, # a lambda function. Here add_multiply

Also, # will store lambda x : multiply(add(x))

Another important point- print("Adding 2 to 5 and multiplying the result with 2: ",

Finally, Output:

Adding 2 with 5 and multiplying the result with 2: 14

### Composing N Number of Functions

It is possible to compose any number of functions by undertaking the modification of the above method.

# Function to combine two

Furthermore, # function which it accepts

Moreover, # as argument

def composite_function(f, g):

return lambda x : f(g(x))

Also, # Function to add 2

return x + 2

# Function to multiply 2

def multiply(x):

return x * 2

Also, # Function to subtract 2

def subtract(x):

return x - 1

# Composite function returns

Furthermore, # a lambda function. Here

Moreover, # add_subtract_multiply will

Also, # store lambda x : multiply(subtract(add(x)))

Another important point- add_subtract_multiply = composite_function(composite_function(multiply,

subtract),

A very important point- print("Adding 2 to 5, then subtracting 1 and multiplying the result with 2: ",

Finally, Output:

Adding 2 with 5, afterwards subtracting 1 and multiplying the result with 2: 12

Now, the modification of the composite_function to a function that can compose any number of functions can take place.  This is made possible by using reduce() function from functools library.

# importing reduce() from functools

from functools import reduce

Furthermore, # composite_function accepts N

Moreover, # number of function as an

Also, # argument and then compose them

def composite_function(*func):

def compose(f, g):

return lambda x : f(g(x))

return reduce(compose, func, lambda x : x)

# Function to add 2

return x + 2

# Function to multiply 2

def multiply(x):

return x * 2

Furthermore, # Function to subtract 2

def subtract(x):

return x - 1

Moreover, # Here add_subtract_multiply will

Also, # store lambda x : multiply(subtract(add(x)))

subtract,

Another important point- print("Adding 2 to 5, then subtracting 1 and multiplying the result with 2: ",

Finally, Output:

Adding 2 to 5, afterwards subtracting 1 and multiplying the result with 2: 12

Explanation:

The first two functions are being taken by the reduce() function from *func. Afterwards, the reduce() function is composing them by using the compose().

Then the composition of the third function takes place to the previous composed function and so on. Here, the composition of the multiply() and subtract() happens first (multiply(subtract(x)) and afterwards the add() is composed (multiply(subtract(add(x))).

### Understanding Composition (Has-A Relation)

Composition (Has-A Relation) is a fundamental and basic concept of Object-Oriented Programming. Furthermore, this concept describes a class that references to one or more objects belonging to other classes as an instance variable.

Here, by creating the object or by making use of the class name, one can access the members of one class inside another class. Furthermore, it enables the creation of complex types by combining objects of different classes.

What this all means is that a class composite can involve another class component’s object. Most noteworthy, experts call this type of relationship as Has-A Relation.

The representation of figure classes can take place as boxes with the class name Composite and Component. This is certainly the representative of Has-A relation between both of them.

class A :

# variables of class A

Furthermore, # methods of class A

...

...

class B :

Moreover, # by using "obj" we can access member's of class A.

obj = A()

Also, # variables of class B

# methods of class B

...

...

Example:

class Component:

# composite class constructor

def __init__(self):

Another important point- print('Component class object created...')

Furthermore, # composite class instance method

def m1(self):

Moreover, print('Component class m1() method executed...')

class Composite:

# composite class constructor

def __init__(self):

Moreover, # creating object of component class

self.obj1 = Component()

Furthermore, print('Composite class object also created...')

# composite class instance method

def m2(self):

A very important point to remember- print('Composite class m2() method executed...')

Furthermore, # calling m1() method of component class

self.obj1.m1()

Moreover, # creating object of composite class

obj2 = Composite()

Also, # calling m2() method of composite class

obj2.m2()

Finally, Output:

Component class object created…

Furthermore, Composite class object also created…

Also, Composite class m2() method executed…

Component class m1() method executed…

Explanation:

In the above example, the creation of two classes Composite and Component takes place in order to show the Has-A Relation among them.

## FAQs For Composition

Question 1: Differentiate between composition and inheritance?

Answer 1: The use of inheritance takes place where a class has to derive the parent class’s nature and afterwards modify the functionality of it. Furthermore, the functionality with extra features will be extended by inheritance, thereby allowing the overriding of methods.

With composition, one can only use the class that cannot modify the functionality of it. Therefore, when one has a need to use the class without any modification, the composition must be the preference.  In contrast, inheritance must be the preferred choice in order to change the behaviour of the method in another class.

Question 2: What is meant by main () in Python?

Answer 2: Python main function is any program’s starting point. Furthermore, when the program is run, the code runs sequentially due to the Python interpreter. Most noteworthy, the execution of the main function takes place only when it is run as a Python program.

Share with friends

## Customize your course in 30 seconds

##### Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Biology
Dr. Nazma Shaik
VTU
Chemistry
Gaurav Tiwari
APJAKTU
Physics
Get Started