use import *

Ian Kelly ian.g.kelly at gmail.com
Mon Aug 1 11:59:47 EDT 2016


On Mon, Aug 1, 2016 at 9:31 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> Hi Team ,
>
> I am a Linux user on python 2,6 . I have a very simple question
>
> I was going the zen of python by Tim peters and found an example  that
> demonstrates Explicit is better than implicit
>
> """Load the cat, dog, and mouse models so we can edit instances of them."""
> def load():
>     from menagerie.cat.models import *
>     from menagerie.dog.models import *
>     from menagerie.mouse.models import *
> #-----------------------------------------------------------------------
> def load():
>     from menagerie.models import cat as cat_models
>     from menagerie.models import dog as dog_models
>     from menagerie.models import mouse as mouse_models
> #-----------------------------------------------------------------------
> print 'Explicit is better than implicit.'

These aren't equivalent. The first implies the existence of the
package "menagerie.cat.models" while the second implies the existence
of "menagerie.models.cat".

> I had a question on the above example
>
> 1. I haven't used " from menagerie.cat.models import *  is it a good
> programming practice to use  import * ?   if answer is "NO " then  are
> there situation where you are forced to use import *

Generally, no. It can be convenient for use in the interactive
interpreter or in toy programs where code health is unimportant. If
you use it in real work, then you end up in situations where you can't
tell just from looking at the source what names are actually being
imported.



More information about the Python-list mailing list