I have just learnt about importing modules, and I am a bit confused about the wildcard import.
from module_name import *
I do not understand the reason of using it at all, I see people saying not to use it at all.
Could someone clarify what it really means, and why would one use it?
This is used for importing everything from the module. The reason why you are recommended not to use it, is because it can get confusing as to where the function or class you are using came from. Moreover, some things might have the same name in different modules, and importing them like this would overwrite the one previously imported.
from module import *
generally imports evey name from a given module (though the module may use__all__
to restrict that). It's generally best avoided, since this set of names may change over time, possibly changing the names available to your code.I do sometimes use it in interactive sessions as a convenience, though.
According to [Python 3.Docs]: Modules - More on Modules (emphasis is mine):
So, it means: importing all (check the above page for the __all__ variable meaning) symbols exported by a module / package into the current namespace.
Generally (as the above states), it's used when one is in the console and wants to save time by not importing everything needed, "manually".
It's also used by some who don't know what to import (so they import everything, since they don't really know what they're doing - there are exceptions of course, but those are rare).
Anyway, probably this is the most eloquent example (as it only relies on Python): illustrating its pitfalls:
The wildcard import shadows:
by:
Things can get even messier when dealing with 3rd-party modules (where the collision hit chance can grow exponentially).