第二段中的 count+=x 表达式是一个赋值表达式,这会创建一个本地的count变
量,但是此表达式中又需要引用count,即count = count + x,这就是错误的由来
第一段中对count仅仅是引用,没有赋值操作,count[0]+=x 是赋值给count的第一
个元素,该元素已经存在,没有错误
在 2009-07-31五的 17:50 +0800,Mu Qiao写道:
Python的文档这么说的:
A scope defines the visibility of a name within a block. If a local variable is defined in a block, its scope includes that block. If the definition occurs in a function block, the scope extends to any blocks contained within the defining one, unless a contained block introduces a different binding for the name. The scope of names defined in a class block is limited to the class block; it does not extend to the code blocks of methods.
count += x 就相当于对count重新bind了,所以count被认为是局部变量。On 7月31日, 下午11时13分, Heroboy <yangwei...@gmail.com> wrote:
> 4.1 Naming and binding
>
> Python的文档这么说的:
>
> A *scope* defines the visibility of a name within a block. If a local
> variable is defined in a block, its scope includes that block. If the
> definition occurs in a function block, the scope extends to any blocks
> contained within the defining one,* unless a contained block introduces a
> different binding for the name*. The scope of names defined in a class block
> is limited to the class block; it does not extend to the code blocks of
> methods.
> count += x 就相当于对count重新bind了,所以count被认为是局部变量。
>
> 2009/7/31 mu <qiao...@gmail.com>