Correct Use Of Global Variables In Python 3

后端 未结 4 1837
故里飘歌
故里飘歌 2020-12-12 20:48

Which is the correct use of global variables in Python 3?:

1) Stating global VAR_NAME once in the core script (not within a function) and then simply re

4条回答
  •  温柔的废话
    2020-12-12 21:19

    The main difference between the first two cases and the next two cases in the above answer would have to be the fact that the list is mutable. For cases like a = 1 a pointer points to the location where 1 is and when you say a = 2 the pointer shifts.

    For the case of mutable objects a memory location is allotted and when methods like append are used changes occur to the memory location itself and so the value the mutable references is changed globally.

    Now the big question is as to how the function knows the variable we are modifying is a global one or local one because it seems we can modify the global variable if its mutable and we cannot if its non mutable (The function also does not recognize this as the global variable)

提交回复
热议问题