Skip to main content
 首页 » 编程设计

python之处理同名类、模块和包的 Pythonic 方式是什么

2025年05月04日15pengyingh

我有一些代码看起来像是愚蠢的名称间距。这是一个精简的例子:

/genelist 
    genelist.py 
        - class GeneList 
    helper1.py 
    helper2.py 
    ... 

GeneList 是我想在整个程序中使用的唯一符号。该类委托(delegate)给包内的其他实用函数。问题是有时我需要像这样引用类:

gl = genelist.genelist.GeneList()

这看起来很傻。是否有更 Pythonic 的方式来组织我的代码(或命名我的组件)以减少样板文件?

编辑:我需要为 circular imports 命名空间.

请您参考如下方法:

你在你的基因列表模块(文件夹)中使用/genelist/__init__.py

from genelist import GeneList 

然后在 whatever.py 中

import genelist 
genelist.GeneList()