Is there any differences among the following two statements?
import os
import os as os
If so, which one is more preferred?
The below syntax will help you in understanding the usage of using "as" keyword while importing modules
import NAMES as RENAME from MODULE searching HOW
Using this helps developer to make use of user specific name for imported modules. Example:
import random
print random.randint(1,100)
Now I would like to introduce user specific module name for random module thus I can rewrite the above code as
import random as myrand
print myrand.randint(1,100)
Now coming to your question; Which one is preferred? The answer is your choice; There will be no performance impact on using "as" as part of importing modules.
import ... as ... from is invalid.The import .... as syntax was designed to limit errors.
This syntax allows us to give a name of our choice to the package or module we are importing—theoretically this could lead to name clashes, but in practice the as syntax is used to avoid them.
Renaming is particularly useful when experimenting with different implementations of a module.
Example: if we had two modules ModA and ModB that had the same API we could write import ModA as MyMod in a program, and later on switch to using import MoB as MyMod.
In answering your question, there is no preferred syntax. It is all up to you to decide.
import sqlite3 as sql and code everything as sql.Xxxxx. Later on I can change import to import MySql as sql and not have to change hundreds or thousands of lines of code which stay as sql.Xxxxx. Good answer.
as osx = 3andx = 3; x = x. The second version is completely pointless; if you're wondering why the second version is something you can even write, it is because no Turing-complete programming language can exclude the possibility of writing pointless things.dis.dis('import os')anddis.dis('import os as os')shows identical bytecode being produced - therefore they are functionally identical - there is now a valid use case - see thread.