0

Is there any differences among the following two statements?

import os
import os as os

If so, which one is more preferred?

MadhuraShree V N
  • 130
  • 1
  • 17
  • Do you know the difference between those two statements? – MadhuraShree V N Jul 17 '15 at 06:31
  • 1
    Yes, the second is longer and slightly slower because of completely redundant `as os` – vaultah Jul 17 '15 at 06:33
  • You can check out this previous answer for some more background, but in your example there is no functional difference. http://stackoverflow.com/a/193931/5066845 – Steven Correia Jul 17 '15 at 06:36
  • 2
    It's the same as the difference between `x = 3` and `x = 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. – user2357112 Jul 17 '15 at 06:36
  • All the comments about the second one longer and results in extra assignment are all wrong - `dis.dis('import os')` and `dis.dis('import os as os')` shows identical bytecode being produced - therefore they are functionally identical - there is now a valid use case - see [thread](https://stackoverflow.com/questions/72504576/why-use-from-module-import-a-as-a-instead-of-just-from-module-import-a). – metatoaster May 02 '23 at 23:37

5 Answers5

5

It is just used for simplification,like say

import random
print random.randint(1,100)

is same as:

import random as r
print r.randint(1,100)

So you can use r instead of random everytime.

cold_coder
  • 584
  • 4
  • 8
  • 24
1

Is there any differences among the following two statements?

No.

If so, which one is more preferred?

The first one (import os), because the second one does the exact same thing but is longer and repeats itself for no reason.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
0

If you want to use name f for imported module foo, use

import foo as f
# other examples
import numpy as np
import pandas as pd

In your case, use import os

f43d65
  • 2,264
  • 11
  • 15
0

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.

Viswesn
  • 4,674
  • 2
  • 28
  • 45
0

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.

BattleDrum
  • 798
  • 7
  • 13
  • 2
    Don't know why you were downvoted but I upvoted. Basically I can write `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. – WinEunuuchs2Unix Dec 17 '18 at 02:06