Are variables in python functions reinitialized with each function call? -
assume have 2 functions
def myfunction1(number): biglist = [1,2,3,4,5,6,7,8,9] print number*biglist biglist = [1,2,3,4,5,6,7,8,9] def myfunction2(number, biglist): print number*biglist
i time them ipython's magic %timeit:
in [5]: %timeit myfunction2(number, biglist) 1000000 loops, best of 3: 607 ns per loop in [6]: %timeit myfunction1(number) 1000000 loops, best of 3: 841 ns per loop
does mean biglist
variable re-declared every time call myfunction1? have guessed after first function-call python somehow store biglist
variable function, not have re-initialize list every time function called.
i don't know inner workings of python i'm guessing. can explain happens?
python can't suggest without doing quite complicated analysis. assignment statement assignment statement if type x=3
twice in interpreter expect x 3 after type regardless of have done x in between... no different
to illustrate - function be
def myfunction1(number): biglist = [1,2,3,4,5,6,7,8,9] biglist = number*biglist print biglist
in case want reassign biglist.
this ofcourse ignores fact biglist different variable every call - have func executing on 2 threads @ same time , unrelated
Comments
Post a Comment