Python- Day 27- FUNCTIONS-Storing Functions in Modules

Nidhi Ashtikar
3 min readNov 27, 2024

--

This will be the last topic of Functions in Python

Function Separation:

Functions help organize code, improving readability.

Functions can be stored in separate files (modules) for better code management and reusability.

pizza.py (module file)

# pizza.py

def make_pizza(size, *toppings):

print(f"\nMaking a {size}-inch pizza with the following toppings:")

for topping in toppings:
print(f"- {topping}")

making_pizzas.py (main program)

# making_pizzas.py

import pizza

pizza.make_pizza(16, 'pepperoni')
pizza.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

>>


Making a 16-inch pizza with the following toppings:
- pepperoni

Making a 12-inch pizza with the following toppings:
- mushrooms
- green peppers
- extra cheese

Importing Modules:

Use import module_name to access all functions in a module.

In this case, only the function make_pizza is imported instead of the whole module.

making_pizzas.py (main program)

# making_pizzas.py
from pizza import make_pizza

make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

Using ‘as’ to Give a Function an Alia

If the function name is long or conflicts with another function name, an alias can be used.

making_pizzas.py (main program)

# making_pizzas.py

from pizza import make_pizza as mp

mp(16, 'pepperoni')
mp(12, 'mushrooms', 'green peppers', 'extra cheese')

Using ‘as’ to Give a Module an Alias:

To make the module name shorter, use an alias for the module itself.

making_pizzas.py (main program)

# making_pizzas.py

import pizza as p

p.make_pizza(16, 'pepperoni')
p.make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

Importing All Functions from a Module:

When using the asterisk (*), all functions in the module are available directly by their names.

making_pizzas.py (main program)

# making_pizzas.py
from pizza import *

make_pizza(16, 'pepperoni')
make_pizza(12, 'mushrooms', 'green peppers', 'extra cheese')

Styling Functions and Modules:

Here are styling examples following PEP 8 guidelines:

Module Naming and Function Definition with Docstring:

# pizza_maker.py

def make_pizza(size, *toppings):

#Prepare a pizza of given size with the specified toppings.
print(f"\nMaking a {size}-inch pizza with the following toppings:")

for topping in toppings:
print(f"- {topping}")

PEP 8 Line Length, Indentation, and Import Placement:

# order_pizza.py

from pizza_maker import make_pizza

# Making pizzas with proper line length and indentation

make_pizza(16, 'pepperoni', 'green peppers', 'mushrooms')
make_pizza(
12, 'extra cheese', 'sausage', 'pineapple',
'olives', 'onions', 'bell peppers'
)

These examples show how to create, import, and use functions in modules, and follow best practices to keep code organized and readable.

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

No responses yet