Example: -
- First of all, we need a directory.- The name of this directory will be the name of the package, which we want to create.
- We will call our package "pack1".
- This directory needs to contain a file with the name "__init__.py".
- This file can be empty, or it can contain valid Python code.
- This code will be executed when a package will be imported, so it can be used to initialize a package, e.g. to make sure that some other modules are imported or some values set.
- Now we can put into this directory all the Python files which will be the submodules of our module.
- We create two simple files a.py and b.py just for the sake of filling the package with modules.
The content of a.py:
def a1():
print("Hello, function 'a1' from module 'a' calling")
The content of b.py:
def b1():
print("Hello, function 'b1' from module 'b' calling")
Note: __init__.py is simply a file that is used to consider the directories on the disk as the package of the Python. It is basically used to initialize the python packages.
Some Basic Information About This:-
- A Package is simply a collection of similar modules, sub-packages etc. - A package is basically a directory with Python files and a file with the name __init__.py. This means that every directory inside of the Python path, which contains a file named __init__.py, will be treated as a package by Python.
- It's possible to put several modules into a Package.
- Packages are a way of structuring Python’s module namespace by using "dotted module names". A.B stands for a submodule named B in a package named A.
- Two different packages like P1 and P2 can both have modules with the same name, let's say A, for example.
- The submodule A of the package P1 and the submodule A of the package P2 can be totally different. - A package is imported like a "normal" module.
Demo Programm...
- Write a Python program that creates a package called “Lunch” which consists of fruit.py and vegetable.py. Fruit.py contains 3 function called apple(), banana(), orange(). Vegetable.py contains 2 function called potato() and tomato(). Create one main file outside the lunch package and import Lunch to access fruit.py and vegetable.py file using __init __.py file. Call all the functions in main file using the concept of Package.
2.py Prog.name
from Lunch import Fruit,Vegetable
Fruit.apple()
Fruit.banana()
Fruit.orange()
Vegetable.potato()
Vegetable.tomato()
_init_ Blank file
Create Fruit.py File And write This
def apple():
print 'Apple'
def banana():
print 'Banana'
def orange():
print 'Orange'
Write IN vegetable.py file
def potato():
print 'Potato'
def tomato():
print 'Tomato'
If you Need More Demo then Comments I will upload PDF FILE So You Can easily Understands
5 Comments
WcappecllitzoManchester Tracy Jobin https://marketplace.visualstudio.com/items?itemName=orinsulno.Descargar-Fallen-Region-gratuita
ReplyDeletegbasinkaslant
globpirecmi_1994 Sean Kalina https://www.quartersslc.com/profile/venedyctophrahvenedyct/profile
ReplyDeletekefucoself
riapaex_de Willie Wingfield Download
ReplyDeletengotsuwonal
tituKilshi-Baltimore Chris Henderson https://colab.research.google.com/drive/1n9SjPBS512SF3BXVeNu7jNgKUV8LLZ6h
ReplyDeleteclick here
click here
link
opidtuare
stagliFpersba_Baltimore Brandon Chandler KMPlayer 2022.8.25.13 + 64 bit
ReplyDeleteUltraISO
WinRar
iMyFone LockWiper 7.4.1.2
trypenudbou