Why is it bad practice to use from module import *?

1.2k views Asked by At

As a 'habit', almost everyone in stackoverflow and example pages use import numpy as np and then type

t = numpy.arange(0,40000,4000)

Why don't we/Why is it bad practice to use from numpy import * and then type

t = arange(0,40000,4000)

Please give me reasons. (My guess: 1. In case we need to import more than one module, some functions in different modules share the same name. 2. At import module == from module import *?, I can see that this 'habit' results in faster processing time.) What are some other reasons?

1

There are 1 answers

0
alexis On BEST ANSWER

This is what the Python style guide says about it:

Wildcard imports ( from <module> import * ) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. There is one defensible use case for a wildcard import, which is to republish an internal interface as part of a public API (for example, overwriting a pure Python implementation of an interface with the definitions from an optional accelerator module and exactly which definitions will be overwritten isn't known in advance).