patchcas.blogg.se

Packages in python
Packages in python









While writing code or working on a program you need to use many functions, classes, and variables as we have to avoid repetition of the code so we make these functions but the code may be very large which may lead to a large number of functions and classes in the code and this may create confusion so in order to reduce the size of the code we group together some similar classes, functions, and variables and that collection is modules in python and to arrange the related modules into a single hierarchy we use packages in python and it can be referred as the collection of sub-packages and modules. What is the Need for Modules and Packages in Python? By going through the whole article you will have a complete idea of python modules and packages. You can also define different Python classes in those files and then you can create your packages out of those classes.In this article we will learn about modules and packages in python, we will first have a basic introduction to modules and packages in python followed by an individual discussion on what is package in python and what is a module in python, then we will see how to create python package, and module in python followed by the difference between package and module in python and last the main reasons for the difference between module and package in python.

packages in python

In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. When the above code is executed, it produces the following result − I'm Pots Phone To make all of your functions available when you've imported Phone, you need to put explicit import statements in _init_.py as follows − from Pots import PotsĪfter you add these lines to _init_.py, you have all of these classes available when you import the Phone package. Now, create one more file _init_.py in Phone directory −

  • Phone/Isdn.py file having function Isdn().
  • Similar way, we have another two files having different functions with the same name as above −

    packages in python

    This file has following line of source code − #!/usr/bin/python A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on.Ĭonsider a file Pots.py available in Phone directory.











    Packages in python