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

xslt - Substring before throwing error -

Google Cloud Bigtable Durability/Availability Guarantees -

php variable $_SERVER['SERVER_ADDR'] is blank when using crontab -