When we import a module, say os, aren't we importing everything in it?
Then what's the use of from moduleName import (delimiter) should be added to the file in order for us to use its constants? and bunch of other things.
Can any one explain the exactly what from moduleName does when we actually have already loaded the module using import?
When you just do import sys for example, you do input everything in it. When you do a from sys import exit you import that specific module to be used without its first module name. Basically, if you use the from sys import exit statment you can just call:
exit()
Instead of:
sys.exit()
It's just a way to save less time writing the full sys.exit() statement. If you use it to load constants, you just allow yourself to write shorter statements to write something. If you have questions just ask!
Suppose I want to use os.path.abspath. I can import os, and type os.path.abspath every time I want to use it. Or I can write from os.path import abspath, and now I just need to type abspath.
The utility of something like:
import os
from os.path import abspath
Is that I can still reference other objects defined in os, like os.path.splitext, but if I use abspath frequently, I only need to type abspath.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With