Python:重新加载使用“from X import Y"导入的组件 Y? [英] Python: reload component Y imported with 'from X import Y'?

查看:64
本文介绍了Python:重新加载使用“from X import Y"导入的组件 Y?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中,一旦我使用 import X 在解释器会话中导入了模块 X,并且模块在外部发生了变化,我可以使用 reload(X)<重新加载模块/代码>.然后这些更改会在我的口译会话中可用.

In Python, once I have imported a module X in an interpreter session using import X, and the module changes on the outside, I can reload the module with reload(X). The changes then become available in my interpreter session.

我想知道当我使用 from X import Y 从模块 X 导入组件 Y 时是否也可以这样做.

I am wondering if this also possible when I import a component Y from module X using from X import Y.

语句 reload Y 不起作用,因为 Y 不是模块本身,而只是模块内部的一个组件(在本例中是一个类).

The statement reload Y does not work, since Y is not a module itself, but only a component (in this case a class) inside of a module.

是否有可能在不离开解释器会话(或导入整个模块)的情况下重新加载模块的各个组件?

Is it possible at all to reload individual components of a module without leaving the interpreter session (or importing the entire module)?

为了澄清,问题是关于从模块 X 导入类或函数 Y 并重新加载更改,而不是从包 X 导入模块 Y.

For clarification, the question is about importing a class or function Y from a module X and reloading on a change, not a module Y from a package X.

推荐答案

如果 Y 是一个模块(X 是一个包)reload(Y) 就可以了 -- 否则,你会看到为什么好的 Python 风格指南(例如我雇主的)说永远导入任何除了模块(这是众多重要原因之一——但人们仍然继续导入函数和类直接,无论我如何解释它不是一个好主意;-)

If Y is a module (and X a package) reload(Y) will be fine -- otherwise, you'll see why good Python style guides (such as my employer's) say to never import anything except a module (this is one out of many great reasons -- yet people still keep importing functions and classes directly, no matter how much I explain that it's not a good idea;-).

这篇关于Python:重新加载使用“from X import Y"导入的组件 Y?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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