Why does simply importing a python module executes everything present in that module ? [duplicate]

喜欢而已 提交于 2020-01-03 18:33:13

问题


Recently I noticed that using modules was a good option to keep my python programming tidy. For getting started, I made one module (named, oop.py) with a single class in it, which looks like below:

#Module named oop

class Team:

    def __init__(self):
            print "class Team initialized"

    def displayTeam(self):
            print "Team name: ", self.name, ",Rank :" , self.rank

    def setTeam(self,name,rank):
            self.name = name
            self.rank = rank


t1 = Team()
t1.setTeam("Man-Utd", 1)
t1.displayTeam()

According to python documentation, if we want to use specific attribute from a module then we use <from module_name> import <attribute> . I wanted to only load the "class Team"

In another python code (named, oop1.py) I simply imported the above module. oop.py is as mentioned below :

#This is oop1.py.          
#Importing module oop

from oop import Team

The output of python oop1.py from terminal was :

class Team initialized
Team name:  Man-Utd ,Rank : 1

By declaring from oop import Team , I was expecting to load only class definition. Why are those extra lines t1 = Team() t1.setTeam("Man-Utd", 1) t1.displayTeam() from oop.py are getting executed ?

Is initialization not allowed in modules ? What should I do if I only want class Team structure and not other stuff of module ? Corerct me if I am wrong somewhere.


回答1:


In python modules are objects. In order to create a module object the code that it contains is executed and the bindings that are found are added to the object as attributes.

Specifying what you want to import doesn't change the fact that the whole module is executed and afterwards only that single binding is put in the scope by the import.

It's standard practice to put any code that you do not want to execute when importing after a guard:

if __name__ == '__main__':
    # your code here

__name__ is a special global that is __main__ only when executing the module. During an import it is set to the name of the module (and thus that code wont be executed).


To learn more about modules read the documentation. There is also a section that explains the above mentioned guard: Executing modules as scripts




回答2:


Yes, the code of every module you import will be run on the initial import. (Since Python is an interpreted language, that's expected behavior.)

If there are parts of the module that you don't want run on imports, only when the file is run directly, do

if __name__ == "__main__":
    t1 = Team()
    t1.setTeam("Man-Utd", 1)
    t1.displayTeam()



回答3:


class Team:

    def __init__(self):
            print "class Team initialized"

    def displayTeam(self):
            print "Team name: ", self.name, ",Rank :" , self.rank

    def setTeam(self,name,rank):
            self.name = name
            self.rank = rank

if __name__ =='__main__': # use if __name__ =='__main__':, then lines will not be executed when you import.
    t1 = Team()
    t1.setTeam("Man-Utd", 1)
    t1.displayTeam()


来源:https://stackoverflow.com/questions/24427090/why-does-simply-importing-a-python-module-executes-everything-present-in-that-mo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!