Keep the lifespan of variable after multiple function calls?

后端 未结 4 1215
抹茶落季
抹茶落季 2021-01-27 17:18

Assuming:

def myfunc(x):
    my_list  = []
    list.append(x)

is there a keyword to stop a variable(my_list) from being reassigned? Let\'s supp

4条回答
  •  独厮守ぢ
    2021-01-27 18:17

    You can declare the variable globally as in C/C++ and use global keyword in python to refer them as variable in global data section. Some of references about them:

    • https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python

    • http://www.geeksforgeeks.org/global-local-variables-python/

    • Use of "global" keyword in Python

提交回复
热议问题