[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

SyntaxError: 'import *' not allowed with 'from .'

George Sakkis

1/14/2008 7:37:00 PM

Unless I missed it, PEP 328 doesn't mention anything about this.
What's the reason for not allowing "from .relative.module import *' ?

George
4 Answers

Ben Finney

1/14/2008 11:01:00 PM

0

George Sakkis <george.sakkis@gmail.com> writes:

> Unless I missed it, PEP 328 doesn't mention anything about this.
> What's the reason for not allowing "from .relative.module import *'
> ?

It makes the code much harder to follow visually and inspect with
static analysis tools, since there's no way to see where names come
from in the code. It defeats the purpose of separate namespaces,
confusing the imported module's names with the current module's names
in a way that makes the indistinguishable.

If you want to use all or most of the names in a module, keep them in
their own namespace:

import spam
import eggs

spam.do_stuff()
eggs.do_stuff()

If you don't like the name of the module, then use whatever one suits
you:

import your_mother_was_a_hamster as spam
import your_father_smelled_of_elderberries as eggs

spam.do_stuff()
eggs.do_stuff()

Both of these are superior to 'from spam import *' because it's clear
(to the reader and to static analysis tools) where every name comes
from: unqualified names must be defined in the current module, any
ones from the imported module are qualified with the module name.

You also, in cases like the above example, avoid unknowingly
clobbering existing names by importing from another module into the
current namespace.

--
\ "Never use a long word when there's a commensurate diminutive |
`\ available." -- Stan Kelly-Bootle |
_o__) |
Ben Finney

George Sakkis

1/14/2008 11:09:00 PM

0

On Jan 14, 6:01 pm, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:

> George Sakkis <george.sak...@gmail.com> writes:
> > Unless I missed it, PEP 328 doesn't mention anything about this.
> > What's the reason for not allowing "from .relative.module import *'
> > ?
>
> It makes the code much harder to follow visually and inspect with
> static analysis tools, since there's no way to see where names come
> from in the code. It defeats the purpose of separate namespaces,
> confusing the imported module's names with the current module's names
> in a way that makes the indistinguishable.
>
> If you want to use all or most of the names in a module, keep them in
> their own namespace:
>
> import spam
> import eggs
>
> spam.do_stuff()
> eggs.do_stuff()
>
> If you don't like the name of the module, then use whatever one suits
> you:
>
> import your_mother_was_a_hamster as spam
> import your_father_smelled_of_elderberries as eggs
>
> spam.do_stuff()
> eggs.do_stuff()
>
> Both of these are superior to 'from spam import *' because it's clear
> (to the reader and to static analysis tools) where every name comes
> from: unqualified names must be defined in the current module, any
> ones from the imported module are qualified with the module name.
>
> You also, in cases like the above example, avoid unknowingly
> clobbering existing names by importing from another module into the
> current namespace.

All the above are well-known and apply to both absolute and relative
imports. I was asking why it's a syntax error specifically for
relative imports.

George

Carl Banks

1/14/2008 11:46:00 PM

0

On Jan 14, 2:37 pm, George Sakkis <george.sak...@gmail.com> wrote:
> Unless I missed it, PEP 328 doesn't mention anything about this.
> What's the reason for not allowing "from .relative.module import *' ?

I'm just guessing: it could accidentally create infinite recursion.
Or, perhaps something more subtle than infinite recursion, such as
hard-to-comprehend rules about what modules and subpackages would be
imported. So they just decided to disallow it. Just a guess.


Carl Banks

Steven Bethard

1/15/2008 6:59:00 PM

0

George Sakkis wrote:
> Unless I missed it, PEP 328 doesn't mention anything about this.
> What's the reason for not allowing "from .relative.module import *' ?

Generally, there's a move away from all "import *" versions these days.
For example, Python 3.0 removes the ability to use "import *" within a
function:

http://www.python.org/dev/peps...

I suspect the "import *" version is not enabled for relative imports
because most of python-dev is against "import *" forms anywhere.

STeVe