3

Why give full path to call a function when import statement is used to import a module from subpackage of package?

eg . A is package , AA is sub package of A , AA1 is module in AA

import A.AA.AA1
A.AA.AA1.AA1fun()

works correctly

but

AA1.AA1fun()

Why not this and why is 'A' got placed in global namespace why not 'AA1'?

jonrsharpe
123k31 gold badges278 silver badges489 bronze badges
asked Jun 21, 2022 at 10:43
3
  • import A.AA.AA1 as AA1 Commented Jun 21, 2022 at 11:10
  • i know about aliasing and from statement but i want to know about the reason behind giving full path in sole import statement? Commented Jun 21, 2022 at 14:39
  • for disambiguation I guess Commented Jun 21, 2022 at 14:48

2 Answers 2

3

Let's suppose that we perform an import of these two modules:

import A.AA.AA1
import B.AA.AA1

Which of the two modules would be chosen if you run the following functions?

AA.AA1.fun()
AA.AA1.fun()
AA1.fun()
AA1.fun()

To avoid this ambiguity you have to explicitly put the whole package, subpackage and module path.

A.AA.AA1.fun()
B.AA.AA1.fun()

If you want to avoid having to use the whole path every time, you have the from option:

from A.AA.AA1 import fun
fun()

But, by doing this, the name of the identifier fun is exposed. Therefore, if fun was already assigned to another object previously, it is overridden and now points to the new object in A.AA.AA1.

fun = lambda x: 2*x
from A.AA.AA1 import fun
from B.AA.AA1 import fun

In this last example, after executing these lines of code, fun refers only to the object in module B.AA.AA1.

You can also use the as option to create an alias to the imported module:

import A.AA.AA1 as AAA1
import B.AA.AA1 as BAA1
AAA1.fun()
BAA1.fun()

This way the whole path is abbreviated and avoids ambiguity when executing fun from one module or another.

In this link you can find the documentation: import doc

answered Jun 21, 2022 at 11:28
Sign up to request clarification or add additional context in comments.

2 Comments

Please explain about namespace too . I.e why ' A ' is added to the global namespace instead of ' AA1 '. Bcz only AA1 is imported in program
@harshit Perfect. I'll leave it pending for when I have some time. Thank you very much.
1

The main difference is in how import works, please refer to the docs section 5.4.2 for more information. As an example:

import A.AA.AA1
A.AA.AA1.AA1fun()

With as sole import you will have to provide the complete path. On the other hand, if you use a from statement you can use it directly:

from A.AA import AA1
AA1.AA1fun()
answered Jun 21, 2022 at 11:11

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.