如果在 Python 中导入自己,为什么不得到无限循环? [英] If you import yourself in Python, why don't you get an infinite loop?

查看:19
本文介绍了如果在 Python 中导入自己,为什么不得到无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是对以下 SO 帖子的回应:

如何腌制对象?>

在那个线程中,OP 不小心在同一个模块的顶部导入了他自己的模块.为什么这不会导致无限循环?

解决方案

模块只导入一次.Python 意识到它已经被导入,所以不会再次导入.

参见:http://docs.python.org/tutorial/modules.html#more-on-modules

This question is a response to the following SO post:

How do I pickle an object?

In that thread, the OP accidentally imports his own module at the top of the same module. Why doesn't this cause an infinite loop?

解决方案

Modules are imported only once. Python realizes it already has been imported, so does not do it again.

See: http://docs.python.org/tutorial/modules.html#more-on-modules

这篇关于如果在 Python 中导入自己,为什么不得到无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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