Importing classes vs importing definitions python

Question:

I have been using python for a while and there is something that I’m missing.

When importing a module, is there any difference if the module contains a class, or just definitions. For example, i have the following two modules:

def hello():
    print("hello")

Or

class Hello():
    def hello():
        print("hello")

And I import it from another module

import module_name

module_name.hello()

Or

import module_name

Hello = module_name.Hello()
Hello.hello()

Is there any difference in the code?

What if a want to make parallel execution? Would i have any issue if I just import the definition?

Asked By: DiegoLamus

||

Answers:

There is nothing wrong with either of them. I would suggest reading https://docs.python.org/3/reference/import.html on how the import system work.

Answered By: ZuperZee

According to Python’s documentation on Modules:

A module is a file containing Python definitions and statements.(…)

Each module has its own private symbol table, which is used as the global symbol table by all functions defined in the module.

Basically when you import a module, a module object instance is created and this private symbol table is allocated and instantiated. This course of action will be the same regardeless of what your module contains.

However, I would not say that both of your example are doing the same thing, because the memory allocation that happens at runtime when you instantiate your class object will be different at a low level than what happens when you simply call your function. This difference shouldn’t be noticible. But if you want to know more about low level memory management you can read here.

Answered By: Aurora Wang

The practical difference when you create class which holds only static methods (explained below) is that you can’t do:

from module_name.Hello import hello

It’s because Hello is not a module. On the other hand you can:

from module_name import hello

or:

from module_name.Hello import hello

if you have a file module_name/Hello.py which contains module attribute hello (i.e. your function).

Another issue is that in given case you don’t really need Hello class object or its instance for Hello.hello to be fully functional. Practically it’s a static method. You waste resources to create unnecessary objects. Module with top level function is the best choice here.

I often say that modules in Python are classes which you can’t instantiate. All things considered they are Singletons.

Also practically speaking hello function and Hello class are module attributes not “definitions”. IMO, the word “definitions” is more appropriate when you are talking about syntax constructs.

Answered By: WloHu

First of all I want to point out that doesn’t use established Python terminology.

You are talking about function hello in first example and class method Hello.hello() in second example.

If you want them to be the same, but you prefer to use classes to organize your functions I’d recommend to use @staticmethod decorator.

Your code will look like this:

class Hello():
    @staticmethod       
    def hello():
        print("hello")

Built-in @staticmethod

Answered By: Sergei
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.