python - Import variable initialization -


i wonder why import variable in python (python 3.4) has different result importing module , referencing, more on why deep copy made - , there way bypass copy (and not defining function returns it)?

a.py

v = 1  def set():     global v     v = 3 

main.py

import import b  a.set() b.foo() 

b.py

from  import  v  def foo():     print(v)     print(a.v)     print(id(v))     print(id(a.v)) 

output

1 3 1585041872 1585041904 

the problem you're modifying scalar value. not problem specific modules, work same when passing variable function , modifying there.

the value 1 imported a, period. whatever in a afterwards not modify value, because it's simple immutable scalar value.

if a.v object, changes object propagate variable holding reference it.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -