module not callable - why not?

Peter Hansen peter at engcorp.com
Fri Apr 9 13:17:09 EDT 2004


djw wrote:
> Diez B. Roggisch wrote:
> 
>> import quaternion
>> q = quaternion.quaternion()
>>
>> Thats a lot to type. doing a
>> from quaternion import quaternion
>> would solve that - but AFAIK thats considered bad for some reasons.
> 
> I think what people consider dangerous is 'from <module> import *'. The 
> form you give here is OK, as far as I know.

Even "from xxx import yyy" can be dangerous if you don't know
what you're doing.  More specifically, any time the yyy thing
might be dynamic (i.e. replace in the original module with
something else at a later time), you will end up with a situation
in which everyone who did the "from xxx import yyy" before the
change has a binding to something other than what xxx.yyy is
currently bound to.

Generally speaking it's safe to do (and so is "from xxx import *")
but considered poor form, extremely so in the latter case.  And
it's not an arbitrary thing: doing this makes code less readable
and maintainable because it becomes unclear where names are
coming from without constant reference to the list of imports
at the top.

-Peter



More information about the Python-list mailing list