Is a module the same as a class in Python?

6

Is a module the same as a class?

If not what are the differences? I ask this because according to The Zen of Python , modules should be used instead of if s.

The problem is that after searching I looked the same.

    
asked by anonymous 05.03.2015 / 02:08

2 answers

9

Module

Roughly a module in Python is a member file. A module can contain classes, functions, and "loose" variables. Modules are organizational units and use a modularization concept.

A module can be imported into other modules by having all its members ( import modulo ) or some selected ( from modulo import classe ) available.

The first form of import only makes members available, the second form still makes it easy to use making explicit reference to what you are using unnecessary in most situations, but it can also create ambiguities. You can still import all members with the second form using from modulo import * .

The name of a module is the name of the file without the extension .py .

There is also a set of modules organized into directories forming what are called packages.

Class

Classes are used to create types, to structure how objects will be defined. Classes have state and state-related behavior, that is, they have properties and methods that do related actions often manipulating the properties in the class. Classes are data structures and use an object-oriented concept.

Classes can be instantiated, that is, you can create objects and save them to variables based on what was defined in it. The class is a model to follow, it's like a low floor plan of a house and the object will be the house built.

Technically classes can also be used as organizational units. In this case the class should not be instantiated and would probably only have class methods and static variables. But this is not the pythonic way of doing since language allows you to get the same result with modules.

The "normal" members of a class can only be accessed through an instance. They do not exist until an object is created based on the class.

Classes can have subtypes, that is, they can have inheritance and create a type hierarchy.

To use a class just call a constructor of it that is confused with its own name:

funcionario = Funcionario("João", 2000) //está criando um objeto com base na classe Funcionario

Wikipedia article on classes .

Conclusion

In a way we can say that the modules are surnames for the classes and other members that form the same family.

Perhaps the idea that they are the same thing comes from the fact that both have members (variables and functions) but the way they will be accessed is quite different. To facilitate the understanding one can imagine a module as being a class that can not be instantiated. In fact and in some languages the module is exactly this. This is not quite the case with Python, but if this thought helps more than it disturbs your understanding, use it.

I do not know where you read which modules should be used in place of ifs , this seems to make as much sense as using gas instead of steel to make a milkshake. In fact this is already weird considering the title of the question that makes sense and I used it to answer. Perhaps some comparison was made to ease understanding and caused more confusion.

    
05.03.2015 / 02:35
4

No. Module can be defined as a file containing definitions of variables, functions and classes , creating a module can be useful in situations where you need to use the same functions and / or classes in other projects without having to write them in each one.

See a demo:

# Modulo1  
# Define variaveis
var1 = 1
var2 = 2

# Define algumas funcoes
def ola():
    return "ola!"

def vezesquatro(numero):
    return int(numero) * 4

# Define uma classe
class Foo:
    def __init__(self):
        self.nome  = raw_input("Qual o seu nome:")
        self.idade = raw_input("Qual a sua idade:")
        self.peso  = raw_input("Qual o seu peso:")
    def info(self):
        print "O nome dele e {0}, tem {1} anos e pesa {2} quilos!".format(self.nome, self.idade, self.peso)

Save script as modulo1.py and create another script and run the following code:

#!/usr/bin/python

import modulo1       

print (modulo1.var1)
print (modulo1.var2)

ola = modulo1.ola()
print (ola)

quatro = modulo1.vezesquatro(10)
print ("10 x 4 = {0}".format(quatro))

f = modulo1.Foo()
f.info()

A class is a structure that abstracts a set of objects that defines their behavior through methods and the possible states of these objects through attributes .

The mgibsonbr answer in the next issue addresses this subject in a broader way.

05.03.2015 / 02:46