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 if its helps in way of explaining. Thanks.
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 if its helps in way of explaining. Thanks.
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 themodule
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:
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.
This is how I decide to organize my code in classes or modules:
Class is supposed to be a blueprint to create (many) instances of objects based on that blueprint. Moreover, classes can have sub-classes (inheritance).
Therefore, if I need inheritance or (many) instantiations, I gather functions and variables under a class definition (methods and properties).
Otherwise, I Keep It Simple and Stupid (KISS) and use modules.
A good indication of a bad class (that should have been a module): you can rewrite your all your object methods and properties with class methods and class properties.
Module:
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, themath
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.
Can I assign value to a variable in the module?
In short yes.
The concept of
module
refers to a single Python file which can be imported (by importing, you have the access to the variables/methods/classes defined in that module).It is commonly discussed together with the concept
package
, which is a folder with__init__.py
. A package can contain sub-packages or modules, and at the same time, similar to modules, can define variables/methods/classes to be imported inside its__init__.py
.The purpose of having modules/packages in Python is kind of similar to having packages in Java: to contain and categorize reusable codes, to resolve naming confliction and so on.
Besides, Python also has a builtin class named
module
, just likelist
,tuple
,dict
(note that Python builtin classes do not follow the CapWords naming convention due to legacy reasons). Amodule
instance represents the module/package imported.When you use the
import
statement to import a module (single Python file) or a package (folder with__init__.py
), typically1. a new instance of
module
class will be created2. the classes/methods/variables you defined in that imported Python file will be added as the attributes of this
module
instance (if it is a package, it will be the classes/methods/variables defined in__init__.py
that are added).Therefore, since it is just an instance of
module
class, you can assign a value to the attribute of that instance and other class instance operations.console: