Packages
Packages
Packages
What is a Package?
o A package is a container that contains various functions to perform
specific tasks.
o While working on big projects, we have to deal with a large amount of
code and writing everything together in the same file will make our
code look messy.
o Instead we can separate our code into multiple files by keeping the
related code together in packages.
o We can use package whenever we need it in our projects. This way we
can also reuse our code.
Packages
Creating package:
o Create a folder named mypackage.
Understanding __init__.py:
o __init__.py helps the python interpreter recognize the folder as a
package.
o It also specifies the resource to be imported from the modules.
o If the __init__.py is empty this means that all the functions of the
modules will be imported.
o We can also specify the functions from each module to be made
available.
Packages
We can also import the specific function from the module:
Example: Output:
Packages
Advantages:
• Packages are useful to arrange all the classes & interface
performing the same task.
• The classes & interfaces of a packages are isolated from the classes
& interfaces of another packages.