Mega Code Archive

 
Categories / Python / Language Basics
 

Change referenced variable value

#//File: small.py x = 1 y = [1, 2] #////////////////////////////////////////////////////////////////////////// #//Main1.py from small import x, y      # copy two names out x = 42                      # changes local x only     y[0] = 42                   # changes shared mutable in-place #////////////////////////////////////////////////////////////////////////// #//Main2.py import small                      # get module name (from doesn't) print small.x                     # small's x is not my x print small.y                     # but we share a changed mutable #////////////////////////////////////////////////////////////////////////// #//Main3.py from small import x, y            # copy two names out print x = 42                      # changes my x only    #////////////////////////////////////////////////////////////////////////// #//Main4.py import small                      # get module name print small.x = 42                # changes x in other module