Recall & Review
beginner
What is the purpose of the __init__.py file in a Python package?
The __init__.py file tells Python that the directory should be treated as a package. It can also initialize package-level variables or import submodules.Click to reveal answer
beginner
True or False: Without an __init__.py file, Python cannot recognize a folder as a package.
False. Since Python 3.3+, folders without __init__.py can be treated as namespace packages.
Click to reveal answer
intermediate
Can __init__.py contain executable code? What is a common use for this?
Yes, __init__.py can contain executable code. A common use is to initialize package variables or import specific classes/functions to simplify access.Click to reveal answer
beginner
What happens if __init__.py is empty?
If __init__.py is empty, the folder is still recognized as a package, but no initialization code runs.
Click to reveal answer
intermediate
How does __init__.py help with importing submodules in a package?
By importing submodules inside __init__.py, you can make them available directly when importing the package, making code cleaner and easier to use.
Click to reveal answer
What does the __init__.py file do in a Python folder?
✗ Incorrect
The __init__.py file marks the folder as a package so Python can import from it.
Can __init__.py contain code that runs when the package is imported?
✗ Incorrect
Yes, __init__.py can contain code that runs when the package is imported.
If __init__.py is missing, what happens when you try to import the folder as a package?
✗ Incorrect
Since Python 3.3, without __init__.py, Python treats the folder as a namespace package.
How can __init__.py simplify importing submodules?
✗ Incorrect
Importing submodules inside __init__.py allows users to access them directly from the package.
Is it mandatory to have __init__.py in Python 3.12+ packages?
✗ Incorrect
In modern Python, __init__.py is not strictly mandatory but recommended to explicitly mark packages.
Explain the role of the __init__.py file in a Python package.
Think about how Python knows a folder is a package.
You got /4 concepts.
Describe how __init__.py can be used to simplify imports in a package.
Consider how you want to access parts of a package easily.
You got /3 concepts.