CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns

Function Factory In Python

A function factory in python is used to create function objects.

What is Function Factory?

We can have a function works as a factory which creates and returns function. We can return different functions based on parameter of function. As we have a function inside another function so closure is created and inner function will have access to function factory parameter even after it returns. This provides great flexibility for creation of functions. Let's see the implementation of python function factory.

Enroll Now and Get an Early Bird Offer


Python Function Factory Example 1 Source Code


Executing Restart on Linux
Executing Shutdown on Windows

We can also have if statement outside and can create the function object based on value of parameter of factory function.

Python Function Factory Example 2 Source Code


Executing Restart on Linux
Executing Shutdown on Windows

Next Article : Building Framework In Python

Suresh Kumar Srivastava is founder of online learning site and author of popular books "C In Depth", "Data Structures Through C In Depth". He has 18+ years experience in industry and worked on architecture and design of multiple products. This article is from his course on Advanced JavaScript.

Yearly Subscription

Advanced Python

Data Structures in Python

Design Patterns Through Python




Knowledge Hub

Design Patterns

Tech Articles

About Us


Contact Us


CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy