Creating custom modules in Python - Performance & Efficiency
Start learning this pattern below
Jump into concepts and practice - no test required
When we create custom modules in Python, we want to know how the time it takes to run our code changes as our program grows.
We ask: how does using a module affect the speed of our program as it gets bigger?
Analyze the time complexity of the following code snippet.
# custom_module.py
def greet(name):
return f"Hello, {name}!"
# main.py
import custom_module
users = ["Alice", "Bob", "Charlie"] # Example list of users
for user in users:
print(custom_module.greet(user))
This code imports a custom module with a simple function and calls it once for each user in a list.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the greet function inside a loop over the users list.
- How many times: Once for each user in the list.
Each time we add more users, the program calls the greet function more times.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 greet calls |
| 100 | 100 greet calls |
| 1000 | 1000 greet calls |
Pattern observation: The number of operations grows directly with the number of users.
Time Complexity: O(n)
This means the time to run grows in a straight line as the number of users grows.
[X] Wrong: "Importing a module makes the program slower every time I call its functions."
[OK] Correct: Importing happens once at the start; calling functions inside loops is what grows with input size.
Understanding how your code runs when using modules helps you write clear and efficient programs, a skill valued in many coding challenges and real projects.
"What if the greet function itself had a loop over a list of greetings? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a module is
A module is a file containing Python code like functions or classes.Step 2: Identify the purpose of custom modules
Custom modules help organize code and allow reuse in different programs.Final Answer:
To organize and reuse code easily -> Option DQuick Check:
Custom modules = organize and reuse code [OK]
- Thinking modules speed up code execution
- Confusing modules with data storage
- Assuming modules create user interfaces
mymodule?Solution
Step 1: Recall Python import syntax
Python uses the keywordimportto bring in modules.Step 2: Match correct syntax
Onlyimport mymoduleis valid Python syntax for importing a module.Final Answer:
import mymodule -> Option AQuick Check:
Import module = import keyword [OK]
- Using 'include' or 'load' which are not Python keywords
- Trying 'using' which is from other languages
- Misspelling 'import'
math_ops.py with this code:def add(a, b):
return a + b
What will be the output of this code?
import math_ops print(math_ops.add(3, 4))
Solution
Step 1: Understand the function in math_ops.py
The functionaddtakes two numbers and returns their sum.Step 2: Analyze the import and function call
Importingmath_opsallows callingmath_ops.add(3, 4), which returns 3 + 4 = 7.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Confusing string concatenation with addition
- Expecting errors due to import
- Forgetting to call function with parentheses
utils.py with a function greet(). Which of these import statements will cause an error?Solution
Step 1: Understand Python import rules
You can import a module or specific functions from it, but not a function as a submodule.Step 2: Check each option
Options B, C, and D are valid. import utils.greet tries to import a function as a module, which causes ImportError.Final Answer:
import utils.greet -> Option BQuick Check:
Functions are imported, not as submodules [OK]
- Trying to import a function like a module
- Confusing 'from' and 'import' usage
- Using invalid aliases
text_utils.py with a function count_vowels(text) that returns the number of vowels in a string. Which code correctly defines this function?Solution
Step 1: Check vowel counting logic
def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) uses a string with both uppercase and lowercase vowels and counts characters in one line using sum and generator.Step 2: Compare other options
def count_vowels(text): vowels = ['a', 'e', 'i', 'o', 'u'] count = 0 for char in text: if char in vowels: count += 1 return count misses uppercase vowels, def count_vowels(text): vowels = 'aeiou' count = 0 for char in text: if char in vowels: count += 1 return count misses uppercase vowels, def count_vowels(text): vowels = 'AEIOU' count = 0 for char in text: if char.lower() in vowels: count += 1 return count incorrectly checks lowercase char in uppercase vowels string.Final Answer:
def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) -> Option CQuick Check:
Count vowels with case check = def count_vowels(text): vowels = 'aeiouAEIOU' return sum(1 for char in text if char in vowels) [OK]
- Ignoring uppercase vowels
- Checking lowercase char in uppercase vowels string
- Using list instead of string for vowels unnecessarily
