python - Confusion about global and immutable -
i'm confused global
, immutable variable. have code:
class processobject: rr = 0 def b(self): self.rr=5 print("valor: ", self.rr) def c(self): print("final: ", self.rr) def d(self): global rr rr = 3 print("valor: ", rr) print(rr) proce = processobject() proce.b() proce.c() proce.d() proce.c()
and have output:
0 value: 5 final: 5 value: 3 final: 5
but not understand why "c
" value 5 if rr object immutable. , why "d
" using global no mute value of rr
.
this has nothing immutability... anyway:
class processobject: # "rr" lives in `class` statement namespace, # it's accessible 'rr' in namespace. after # `class` statement executed, becomes attribute # of `processobject` class, accessible `processobject.rr`, # , thru instances `processobject().rr`. # rr = 0 def b(self): # creates "rr" instance attribute # on current instance (`self`), shadows # "rr" class attribute self.rr=5 print("valor: ", self.rr) def c(self): print("final: ", self.rr) def d(self): # 2 following lines creates module-global # name "rr", distinct 2 previous # ones. global rr rr = 3 print("valor: ", rr) # prints value of `rr` living in class # statement scope - not value of yet non-existing # global rr print(rr) proce = processobject() proce.b() # creates `proce.rr` instance attribute proce.c() proce.d() proce.c()
but not understand why "c" value 5 if rr object immutable.
it prints '5' because assigned value proce.rr
when calling proce.b()
. you're confusing names , values... rr
not object, it's name bound object. fact it's @ 1 point bound immutable object doesn't mean cannot rebind object (mutable or not, that's irrelevant here).
and why "d" using global no mute value of rr
and here confusing binding (assignment) , mutating. binding (assignment) means "make name points object", mutating means "change state of object". example of mutation adding or removing element to/from list, or reversing list in place.
fwiw, call proce.d
rebind (and bind on first call) module-global "rr".
you may want run '"extended" version of script find out happens:
print("before : globals = {}".format(globals())) class processobject: rr = 0 print "rr defined in class namespace - not in globals: {}".format(globals()) def __init__(self): print("in init") print(" self.__dict__ : {}".format(self.__dict__)) print(" processobject.__dict__ : {}".format(processobject.__dict__)) def b(self): print("before calling b - self.__dict__ : {}".format(self.__dict__)) self.rr=5 print("valor: ", self.rr) print("after calling b - self.__dict__ : {}".format(self.__dict__)) def c(self): print("before calling c - self.__dict__ : {}".format(self.__dict__)) print("final: ", self.rr) def d(self): print("before calling d : globals = {}".format(globals())) global rr rr = 3 print("valor: ", rr) print("after calling d : globals = {}".format(globals())) print(rr) print("after class statement: globals : {}".format(globals())) proce = processobject() proce.c() proce.b() proce.c() proce.d() proce.c()
Comments
Post a Comment