Defining lists as global variables in Python

前端 未结 3 719
日久生厌
日久生厌 2020-12-05 11:13

I am using a list on which some functions works in my program. This is a shared list actually and all of my functions can edit it. Is it really necessary to define it as \"g

3条回答
  •  伪装坚强ぢ
    2020-12-05 12:06

    Yes, you need to use global foo if you are going to write to it.

    foo = []
    
    def bar():
        global foo
        ...
        foo = [1]
    

提交回复
热议问题