Time is exactly the same -
the difference is in the readability that your own code will have -
In the example you put yourself
from biblioteca import *
...
# 300 linhas depois
...
for elemento in a:
fca_coisas(elemento, b)
So - where did "a" and "b" come from?
When you import elements with the specific nme both who is reading your code (in general yourself), and programming tools such as IDEs and code linters, can learn from each other's name (variable, class, or module) that you are using in your code.
A single from x import *
ends with the functionality of any tool that warns you that you are using a non-existent variable, for example - since the tool has no way of knowing if the name you typed was not imported along with the *
In Python 3 inclusive, for the sake of optimizing access to local variables in the default implementation, it became syntax error using a from x import *
within a function - so that the compiler has no way of knowing, before running the code, what the local variables of the function will be, and which ones it would have to search for as non-local or global variables.
Scraped this, let's see your specific ones:
- It is correct to say that importing from a module only what is going
is it more performative than using the asterisk?
No.
- Or does not it
any difference in Python?
This does not make the slightest difference in Python - anyway the target module is compiled in its entirety - even though it has 300 function declarations and you only use one. (but of course there are several ways to write code that, if that will make a difference - and almost never will - you can actually just compile and import whatever you need).
- It's a good idea to use *, even if
Are there things being imported that I will not use?
It's never a good idea to use *
. Or almost never - there are exegetes, such as libraries that display constants or names for interactive work.
Code samples with Pyqt, NumPy, and Pygame often come with from x import *
- but even so, for a midsize project, it's worth using import nome_grande_da_biblioteca as x
, and accessing things as x.CONSTANTE, x.funcao_1
- than using *
.
- There is
performance difference between library form import * and import
library?
No. Except for an import within a function in Python 2.x - then all code in a function that contains a import *
will be slower, because Python can not optimize access to local variables.