0

I have a file called "const_a.py" it has this in it

ONE = 1
TWO = 2

Then I have another const file called "const_b.py" it has this

from const_a import *

THREE = 3
FOUR = 4

Finally I am printing out some values in test.py

from const_b import *

print(ONE)
print(THREE)

That prints out 1 and 3. I would have expected it to error out trying to print ONE. Apparently const_b now has const_a in it.

What is this behavior called? Is it intentional?

martineau
  • 119,623
  • 25
  • 170
  • 301
a.m.
  • 2,108
  • 5
  • 24
  • 29

1 Answers1

4

from const_a import * is effectively equivalent to

import const_a
ONE = const_a.ONE
TWO = const_a.TWO
del const_a

You are creating names in const_b whose values are taken from the identically named variables in const_a.

chepner
  • 497,756
  • 71
  • 530
  • 681