Extending the import statement?
dg96057 at teledomenet.gr
Sat Mar 15 12:40:03 CET 2003
I was writing the __init__.py module for a package of mine and when i
reached to the definition of the __all__ list, this "i want to become an
import extension" idea crossed my mind...
Wouldn't be nice if, similarly to the way the __all__ definition works,
one could import a group of names from a module/package by defining his
own list and passing it as an import argument? Some examples:
from math import * # imports __all__ elements
from math import trigFuncs # imports __all_trigFuncs__ elements
from why_the_hell_i_named_this_like_that import justThese
and so on...
The extended import statement will recognize a group of names in the
same way as the __all__ list or (optionally) can be given an arbitrary
list of names to import.
Maybe this behavior can be implemented by writing a custom import
function, but this is something i don't like doing at all. On the other
hand, one could benefit from this kind of syntax as it
--- can be used inside a function, unlike the "from <xxx> import *" form
--- prevents namespace polution while preserving the functionality
of a "from <xxx> import *" statement
--- makes (sometimes) code more flexible and/or readable
I admit that i myself can't think of a situation where this syntax is
actually necessary, i just see it as a nice addition to the semantics of
the import statement. Besides, i don't think it's a hard-to-implement
feature and IMO it fits nicely with the language from a syntactic point
So, any comments on the above? Maybe someone can think of another use of
/ / \ \
( _| |_ )
\/ \___/ \/
Sometimes, solution is under your nose...
Sneeze carefully and blow with caution!
More information about the Python-list