How does Python handle globals?

后端 未结 2 1869
悲哀的现实
悲哀的现实 2021-01-20 02:19

I\'ve come across some very odd handling of global variables in Python. I was hoping someone can explain and justify these surprises!

A) This code prints 10 as expec

2条回答
  •  悲哀的现实
    2021-01-20 02:34

    The second instance rebinds a, so the compiler generates a local access for it. The other two only read a, and so normal global scope searching is performed.

提交回复
热议问题