Use this code in your module to prevent people using the "from foo import *" syntax with your module.
1 2 3 4 | @apply
class __all__(object):
def __getitem__(self, _):
raise ImportError("Star imports not supported")
|
Tags: import
Use this code in your module to prevent people using the "from foo import *" syntax with your module.
1 2 3 4 | @apply
class __all__(object):
def __getitem__(self, _):
raise ImportError("Star imports not supported")
|
I don't think it's a good idea. You're quite changing the way the language works - if you think some names shouldn't be exported, just set the __all__ name with a list of names that should.
Also, if anybody wants to check for the public API of your module must then resort to its full scope, since there's no way to check __all__ (iterating over __all__ gives the very same importerror as doing the star import).
Don't use apply as a decorator. It's a cute trick, but apply is a deprecated function.
Benjamin, so you are saying I should change the first line to
?
A ringing endorsement.
http://twitter.com/pythoncoders/status/14438397768
Just set __all__ to an empty list if you don't want to export anything. What do you gain by causing error in end-user's stack? FYI you also break the help() feature which now does not know which methods are meant to be public..