0

What if we have a module that contains two functions and we import only one of them, will the other work?For instance:

file test.py

def a(x):print(x) def b():a(12)

At the interpreter:

from test import b

b()

It prints 12.How is this possible?Please pardon my bad formatting that's my first question :).

  • Possible duplicate of [Does Python import statement also import dependencies automatically?](https://stackoverflow.com/questions/29367255/does-python-import-statement-also-import-dependencies-automatically) – MooingRawr Jul 03 '18 at 16:00

1 Answers1

0

Technically there is no such thing as importing a single name from a module; the entire module is imported and then one or more names are copied to the local namespace. Your import is roughly the equivalent of:

import test
b = test.b
del test

Except that at no point is test ever actually in the local namespace (and subsequently is not actually deleted).

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358