Is it possible to modify variable in python that is in outer, but not global, scope?

前端 未结 9 1956
温柔的废话
温柔的废话 2020-11-30 02:55

Given following code:

def A() :
    b = 1

    def B() :
        # I can access \'b\' from here.
        print( b )
        # But can i modify \'b\' here? \'         


        
9条回答
  •  春和景丽
    2020-11-30 03:12

    No you cannot, at least in this way.

    Because the "set operation" will create a new name in the current scope, which covers the outer one.

提交回复
热议问题