Python: Variables aren't re-assigned

前端 未结 2 937
[愿得一人]
[愿得一人] 2021-01-25 00:35

Why aren\'t the variables below (A,B,C,D) changed when tst is called.

A,B,C = 0,0,0
D = 0

def tst():
    A,B,C = 1,2,3
    D = 4
    print(A,B,C,D)

ts         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 01:26

    The variables within the tst method are local, that is, they refer to different values that only exist inside scope of that method. Use the keyword global (as in global A,B,C,D) inside tst to fix the behavior. See an example here and the question here.

提交回复
热议问题