Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11301

Re: __all__

Date 2011-08-12 13:15 -0700
From Ethan Furman <ethan@stoneleaf.us>
Subject Re: __all__
References <mailman.2070.1312906708.1164.python-list@python.org> <4e416705$0$29988$c3e8da3$5496439d@news.astraweb.com> <CACNEfZa6bKmh6Ysdap1hcute92__ga5X94dWx9oO4Vdh6jQL4Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.2231.1313179180.1164.python-list@python.org> (permalink)

Show all headers | View raw


Paul Woolcock wrote:
> The gurus will have to correct me if this is not an accepted practice, 
> but I know some projects (Fabric is the one that comes to mind) will 
> define a submodule specifically for the 'from blah import *' situation. 
> The submodule would be called "api", or something like that, so you can 
> do: 'from mymodule.api import *' to separate the items you want to 
> include in your public api, while still keeping other names importable 
> by 'import blah'

Have I said lately how much I *love* Python?  Programming is fun again!

Thanks, Paul, that was the clue I needed.  Had to do some thinking since 
dbf is back to a single module, and no longer a package... here's what I 
came up with:

-------------------------------------------------------------
class fake_module(object):
     def __init__(self, name, *args):
         self.name = name
         self.__all__ = []
         for func in args:
             name = func.__name__
             self.__dict__[name] = func
             self.__all__.append(name)
     def register(self):
         sys.modules["%s.%s" % (__name__, self.name)] = self
-------------------------------------------------------------

then in my module (towards the bottom since I have to have all my 
functions/classes written that I want to include) I can have

fake_module('api', class1, class2, func1, exception1).register()

Now somebody else who wants to include the classes, exceptions, etc, 
that make up the primary part of the dbf module can do

from dbf.api import *

but

help(dbf)

will continue to list all the other public utility stuff (defined in 
dbf.__all__) normally.

Thanks to all!

~Ethan~

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

__all__ Ethan Furman <ethan@stoneleaf.us> - 2011-08-09 09:34 -0700
  Re: __all__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-08-10 02:57 +1000
    Re: __all__ Ethan Furman <ethan@stoneleaf.us> - 2011-08-12 13:15 -0700

csiph-web