[Python-ideas] name export

Aahz aahz at pythoncraft.com
Sat Apr 4 18:23:11 CEST 2009


On Fri, Apr 03, 2009, spir wrote:
> 
> When I write tool modules that export useful names to client code, I
> usually use __all__ to select proper names. Sure, it's a potential
> source of identifier conflict. I have another custom __*names__ module
> attribute that allows the client at least to control which names are
> defined in the imported module:
>
> # module M
> __Mnames__ = [...]
> __all__ = ["__Mnames__"] + __M_names__
> 
> Then
> 	from M import * ; print __Mnames__
> outputs needed naming information:
> 
> 	from M import * ; print __Mnames__ ; print dir()
> ==>
> 	['a', 'b', 'c']
> 	['__Mnames__', '__builtins__', '__doc__', '__file__', '__name__', 'a', 'b', 'c']
> 
> [Indeed, you'd have the same info with M.__all__, but I find it
> strange to have both "from M import *" and "import M" only to access
> its __all__ attribute. Also, it happens that a module name and it's
> main defined name are identical, like time.time.]

Your problem is that you're using import * -- stop doing that and you
won't have an issue.  The only good use cases for import * IMO are
interactive Python and packages, and in the latter case I don't see why
anyone would need the information you propose except for debugging
purposes.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it."  --Brian W. Kernighan



More information about the Python-ideas mailing list