1

I have some code with what looks like silly name-spacing. Here is a stripped down example:

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

GeneList is the only symbol I'd like to use throughout my program. That class delegates to other utility functions inside the package. The problem is that sometimes I need to reference the class like this:

gl = genelist.genelist.GeneList()

That seems silly. Is there a more Pythonic way to organize my code (or name my components) to reduce the boilerplate?

EDIT: I need to name space for circular imports.

Community
  • 1
  • 1
jds
  • 7,910
  • 11
  • 63
  • 101

2 Answers2

4

you use /genelist/__init__.py in your genelist module(folder)

from genelist import GeneList

then in whatever.py

import genelist
genelist.GeneList()
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
3

You can import the class using from genelist.genelist import GeneList, and reference the class only as GeneList, for example: g1 = Genelist()

Yuri Malheiros
  • 1,400
  • 10
  • 16
  • Right, but I need to namespace for [circular imports](http://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python). – jds Mar 10 '15 at 16:13
  • 4
    circular imports are usually a red flag that you should refactor your code – Joran Beasley Mar 10 '15 at 16:15
  • Let's assume they aren't in this case. – jds Mar 10 '15 at 16:15
  • 2
    but the part of the problem is the circular imports :P ... the right thing to do is fix it .... the sorta ok thing to do is come up with import hacks to allow it ... – Joran Beasley Mar 10 '15 at 16:20
  • Fair enough. I'll reinvestigate and see if the issue is avoidable. – jds Mar 10 '15 at 16:28