Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python __init__u;.py导入还公开了我使用的模块,而不仅仅是我自己的类_Python_Import_Namespaces_Package - Fatal编程技术网

Python __init__u;.py导入还公开了我使用的模块,而不仅仅是我自己的类

Python __init__u;.py导入还公开了我使用的模块,而不仅仅是我自己的类,python,import,namespaces,package,Python,Import,Namespaces,Package,关于python导入,我还没有掌握一些东西。我读了几十篇文章,但没有找到令人满意的答案。情况是这样的: 我正在写一个由几个模块组成的包。假设包名为pack1。在\uuuu init\uuuuu.py文件中,为了公开我在模块中定义的类和函数,我写道: from .module1 import * from .module2 import * ... 现在,在模块1中: from math import sqrt # a tool that I need

关于python导入,我还没有掌握一些东西。我读了几十篇文章,但没有找到令人满意的答案。情况是这样的:

我正在写一个由几个模块组成的包。假设包名为
pack1
。在
\uuuu init\uuuuu.py
文件中,为了公开我在模块中定义的类和函数,我写道:

    from .module1 import *
    from .module2 import *
    ...
现在,在模块1中:

    from math import sqrt  # a tool that I need

    class class1:
         <body>

    class class2:
         <body>
    ....
    class class100:
         <body>
在另一个项目中,我在
pack1
的命名空间中看到
sqrt
。我是否必须在
\uuuu init\uuuuu.py
文件中分别导入100个类中的每一个,以避免这种情况并保持名称空间干净?我是否必须对
\uuuu init\uuuuuu.py
中的inspect模块进行一些黑客攻击,以便识别已定义但未导入的类(我认为这将非常难看)?或者,正如我所怀疑的,我在如何处理模块结构或导入语句方面犯了一些错误?

通配符导入导入在该模块的全局命名空间中定义的所有内容。它不区分“本地”类、导入的模块、函数或变量

有两种解决方法:

  • 导入您想要的内容,而不是使用wildwards。显式优于隐式,根据
    import this
  • 使用特殊的
    \uuuuuuuuuuuuuuuuuuuu
    变量精确定义在导入通配符模块时应导入的内容。看

  • 你为什么不呢?它位于该名称空间中。是的,我表达得很糟糕;我知道事情完全按照预期进行,我的问题是如何进行干净的通配符导入,因为我想要导入许多名称并显式导入它们会很乏味。您正在解释为什么许多Python开发人员认为通配符导入是一种不好的做法的原因之一。
    \uuuu all\uuuu
    变量是我在优雅地处理通配符导入时缺少的部分。非常感谢你!
        import pack1