What Is The Difference Between Python Class, Module, Package

1. Class.

The concept of classe, which occurs in many programming languages, is fundamental to object-oriented programming and is easy to understand. Class is used to abstract the common characteristics of different objects, classify objects that are similar to each other into the same concept according to the similarity principle. Class encapsulates data and operations for future reuse.

Below is an python class example.

class TestClass(Object):

    def func_1(self):

    def func_2(self):

2. Module.

In Python a .py file is considered as a module. After a script file is created, certain functions and variables are defined. You can reuse these functions and variables by importing the module in other files that require these functions or variables. The module name is the file name minus .py suffix.

Modules can also be simply divided into built-in modules and custom modules. Built-in modules are built into Python, such as sys, os and other basic modules. Built-in function dir( module_name ) let you see what data the module defines (include variable name, module name, function name, etc.). dir() function will return all currently defined names when no arguments are present.

~$ python3
Python 3.7.1 (default, Dec 14 2018, 19:28:38) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

>>> dir('django')
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

2.1 Module Search Path.

When importing a module, the interpreter looks for the module in the current package. If not find, it will find it in the built-in module. If not find also, then find the corresponding module file (module_name.py) according to the path in sys.path value.

3. Package.

A package is a hierarchical file directory structure. It defines a python application execution environment consisting of n modules or n subpackages. A package is a directory containing the __init__.py file, which must contain the __init__.py file and other modules or subpackages.

Package can import other packages using import command, or import some of the modules in other package use from + import command.

import django.http

from django.conf.urls import url

The first file in the package directory should be __init__.py. If there is also __init__.py in the subdirectory then it is a subpackage of the current package.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.