0% found this document useful (0 votes)
8 views10 pages

Packages

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Packages

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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

Package Model Structure:


Packages

Creating package:
o Create a folder named mypackage.

o Inside the folder create an empty Python file named __init__.py.


o Then create two files named mod1 and mod2.
Packages
o Write the following code in mod1.py.

o Write the following code in mod2.py.

o Write the following code in __init__.py.


Packages
o Now, Run the __init__.py file.

o We have executed __init__.py only and mod1.py, mod2.py has been


executed.
Packages

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.

• Packages hide the classes & interfaces in a separate subdirectory, so


that accidental deletion of classes & interfaces will not take place.

• The classes & interfaces of a packages are isolated from the classes
& interfaces of another packages.

• Reusability nature of packages makes programming easy.


Thank You

You might also like