user9121776
user9121776

Reputation:

Why variable value change

I have file abcd.py

a=9
def funn():
    print("A")

I import it two times

>>> import abcd as ss
>>> ss.a
9
>>> import abcd as  qq
>>> qq.a
9

But When I change value of a from ss imported file, qq import file value of a is automatically change.

>>> ss.a=4
>>> ss.a
4
>>> qq.a
4

Upvotes: 0

Views: 53

Answers (1)

PM 2Ring
PM 2Ring

Reputation: 55499

Python doesn't actually re-import a module that it's already imported. So when you do

import abcd as qq

you're simply creating another name for the abcd module, which you've already imported as ss. So ss.a and qq.a are just synonyms for the same integer object.

You may find this article helpful: Facts and myths about Python names and values, which was written by SO veteran Ned Batchelder.

Upvotes: 3

Related Questions