Using module as variable

Question:

I am parsing through some code for an SSD simulator. The top module imports the package experiment. It later instantiates an object to this module as follows:

from config_helper import experiment
.
.
.
if __name__ == '__main__':
.
.
    experiment = RunFTL(args)
    experiment.run()

Inside the experiment module, there is an Experiment class that has a main function defined. I am just wondering what is happening in the experiment = RunFTL(args) operation. I am using Python 2.7.

Asked By: wkoz

||

Answers:

It just rebinds the name, much like the x = 2 in

x = 1
x = 2

Before the assignment, experiment in this scope referred to the object imported from config_helper. After the assignment, experiment in this scope refers to the new instance of RunFTL.

Reassigning imported names like this is almost always a bad idea. People expect imported names not to be overridden, plus reassigning a variable to something completely unrelated with its original usage is a bad idea in general.

Answered By: user2357112
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.