from ... import * with __import__ function [英] from ... import * with __import__ function

查看:166
本文介绍了from ... import * with __import__ function的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何做一个相当于“ import * from module”使用Python的导入功能?

我该如何?使用 __ import __ 函数执行from ... import *?
原因是我在运行时只知道文件的名称,并且在该文件中只有1个类。

How would I do the from ... import * with the __import__ function? The reason being that i only know the name of the file at runtime and it only has 1 class inside that file.

推荐答案

别。 只是不。我需要解释它有多可怕吗?动态导入(虽然有时是不可避免的)并导入到全局命名空间(总是可以避免,但有时更简单的解决方案和精细的信任模块)本身就足够糟糕了。动态导入到全局命名空间是一个噩梦(并且可以避免)。

Don't. Just don't. Do I need to explain just how horrible that it? Dynamically importing (though sometimes inevitable) and importing into global namespace (always avoidable, but sometimes the easier solution and fine withtrusted modules) are bad enough themselves. Dynamically importing into global namespace is... a nightmare (and avoidable).

只需执行 _temp = __import __(name,globals(),locals (),[name_of_class]); your_class = _temp.name_of_class 。根据文档,这是关于名称导入name_of_class的内容做。

Just do _temp = __import__(name, globals(), locals(), [name_of_class]); your_class = _temp.name_of_class. According to the docs, this is about what from name import name_of_class would do.

这篇关于from ... import * with __import__ function的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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