为什么简单地导入 python 模块会执行该模块中存在的所有内容? [英] Why does simply importing a python module executes everything present in that module ?

查看:70
本文介绍了为什么简单地导入 python 模块会执行该模块中存在的所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我注意到使用模块是一个不错的选择,可以让我的 Python 编程保持整洁.首先,我创建了一个模块(名为 oop.py),其中包含一个类,如下所示:

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()

根据python文档,如果我们想使用模块中的特定属性,那么我们使用<from module_name>导入 <属性> .我只想加载班级团队"

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"

在另一个 python 代码(命名为 oop1.py)中,我只是导入了上述模块.oop.py 如下所述:

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

终端的 python oop1.py 输出是:

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

通过声明 from oop import Team ,我期望只加载类定义.为什么那些额外的行 t1 = Team()t1.setTeam("曼联", 1)t1.displayTeam()来自 oop​​.py 的 正在执行?

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.

推荐答案

在 python 中模块是对象.为了创建一个模块对象,它包含的代码被执行并且找到的绑定被作为属性添加到对象中.

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.

指定要导入的内容不会改变整个模块被执行的事实,并且之后仅将单个绑定放入import 的作用域中.

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__ 是一个特殊的全局变量,只有在执行模块时才是 __main__ .在导入期间,它被设置为模块的名称(因此不会执行该代码).

__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

这篇关于为什么简单地导入 python 模块会执行该模块中存在的所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆