Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Module and Class in Python

Can I assign value to a variable in the module? If yes, what is the difference between a class and module?

PS: I'm a Java guy (in case it helps in the way of explaining). Thanks.

like image 891
liwevire Avatar asked Apr 03 '17 11:04

liwevire


People also ask

What is the difference between a class and a module?

What is the difference between a class and a module? Modules are collections of methods and constants. They cannot generate instances. Classes may generate instances (objects), and have per-instance state (instance variables).

Can Python modules contain classes?

A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use.

What is the difference between module and method in Python?

"method" is a function that is a attached to an object or class. A module is a group of defined functions, classes, consrants, etc. In Python, you create a module by simply putting related stuff in a single file.

What is the difference between class module and package in Python?

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. ... 2 Module. In Python a .py file is considered as a module. ... 3 Package.

What is the difference between a class and a module?

The difference between a class and a module in python is that a class is used to define a blueprint for a given object, whereas a module is used to reuse a given piece of code inside another program. A class can have its own instance, but a module cannot be instantiated.

What is a Python module?

A python module is nothing but a package to encapsulate reusable code. Modules usually, but not always, reside in a folder with a __init__.py file inside of it. Modules can contain functions but also classes.

What is a class in Python?

A class contains variables and functions which act on the objects. To define a class, we use the keyword class to do so. A class can be defined in the following manner:


2 Answers

There are huge differences between classes and modules in Python.

Classes are blueprints that allow you to create instances with attributes and bound functionality. Classes support inheritance, metaclasses, and descriptors.

Modules can't do any of this, modules are essentially singleton instances of an internal module class, and all their globals are attributes on the module instance. You can manipulate those attributes as needed (add, remove and update), but take into account that these still form the global namespace for all code defined in that module.

From a Java perspective, classes are not all that different here. Modules can contain more than just one class however; functions and any the result of any other Python expression can be globals in a module too.

So as a general ballpark guideline:

  • Use classes as blueprints for objects that model your problem domain.
  • Use modules to collect functionality into logical units.

Then store data where it makes sense to your application. Global state goes in modules (and functions and classes are just as much global state, loaded at the start). Everything else goes into other data structures, including instances of classes.

like image 136
Martijn Pieters Avatar answered Sep 22 '22 03:09

Martijn Pieters


  • Module:

    A module is a file containing Python definitions and statements.

As the doc say.

So a module in python is simply a way to organize the code, and it contains either python classes or just functions. If you need those classes or functions in your project, you just import them. For instance, the math module in python contains just a bunch of functions, and you just call those needed (math.sin). Just have a look at this question.

On the other hand a python class is something similar to a java class, it's only structured in a slightly different way.

like image 39
lch Avatar answered Sep 20 '22 03:09

lch