1

Whats the difference between the following statements:

  1. import os

  2. from os import *

Do they mean one and the same thing? If so, why two ways of saying it?

asked Jul 19, 2019 at 13:51

2 Answers 2

1

import os

brings the name os into the namespace and thus the os name becomes unique.

so using os.read(fd, n) will read n bytes from the file descriptor fd.

from os import *

brings all the names from the module os into the global namespace. Thus we can use read(fd,n) directly.

Problem with from os import * :

If we have our own function read(fd,n) in the local namespace, by using from os import * we get 2 functions with the same name, and the interpreter uses the local function with that name.

If we create a module with a read(fd,n) function having the same name as the one in os module (both function names will be in the global namespace), the interpreter will only use one of them.

answered Jul 19, 2019 at 14:19
Sign up to request clarification or add additional context in comments.

Comments

1

from os import * imports all methods from os, and should be avoided.

import os just import the namespace and allows you to call methods from os by prefixing with os.

answered Jul 19, 2019 at 13:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.