+ 7

Why does 'from module import *' exist when you can just use 'import module'?

13th May 2016, 4:21 PM
Bas Bothof
Bas Bothof - avatar
7 Answers
+ 12
Main difference is that you refer directly to function or costant in code for example: If you import from math import pi you can write print(pi) If you import module import math: print(math.pi) Less typing. Also I supose it makes your program smaller when you import exactly what you need instead whole module.
14th May 2016, 10:53 PM
Bartosz Konieczny
Bartosz Konieczny - avatar
+ 5
if you import all the functions and methods from a particular module then the list of reserved words increases. So there is a higher chance of your variable name to be a reserved word. Also, you can type only the function or method names without including the module name throughout the code if you import the whole module. So, not importing the whole module helps avoid confusion, but importing the whole module helps u avoid typing the module name everytime but takes up more memory.
21st Jun 2016, 7:40 PM
Gyanendra Dhanee
Gyanendra Dhanee - avatar
+ 3
Both ways are supported for a reason: there are times when one is more appropriate than the other. import module: nice when you are using many bits from the module. drawback is that you'll need to qualify each reference with the module name. from module import ...: nice that imported items are usable directly without module name prefix. drawback is that you must list each thing you use, and that it's not clear in code where something came from. Which to use depends on which makes the code clear and readable, and has more than a little to do with personal preference. I lean toward import module generally because in the code it's very clear where an object or function came from. I use from module import ...when I'm using some object/function a lot in the code.
7th Dec 2016, 11:01 AM
Sourav Kumar Singh
Sourav Kumar Singh - avatar
0
so that you can write sqrt(), instead of math.sqrt()
22nd Jun 2016, 4:58 AM
amit pandya
amit pandya - avatar
0
you specially use it when you are working with a module such as TKinter which allows you to create GUIs. It makes using module variables easier rather than in becoming one big mess
22nd Jun 2016, 4:13 PM
Jonathan de Kock
Jonathan de Kock - avatar
0
Suppose you have to use a function from a module which has a very absurd name . If you directly import the module , then each time you will be required to type that function name which can cause different types of errors. On the other hand , if you write from *module_name* import *functionname* as *a_name_of_your_choice* then each time you will be required to only write the name which you have given to that function , thus leaving less space for errors.
27th Jan 2018, 12:33 PM
Vivek Kumar Gupta
Vivek Kumar Gupta - avatar
- 1
🍅
6th Dec 2016, 9:09 PM
Aj Sanquedolce
Aj Sanquedolce - avatar