On Fri, 2007-05-04 at 14:30 -0700, [EMAIL PROTECTED] wrote: > Hi, > > Can anyone explain the following: > > Python 2.5 (r25:51908, Apr 9 2007, 11:27:23) > [GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> def foo(): > ... x = 2 > ... > >>> foo() > >>> def bar(): > ... x[2] = 2 > ... > >>> > >>> bar() > Traceback (most recent call last): > File "<stdin>", line 1, in <module> > File "<stdin>", line 2, in bar > NameError: global name 'x' is not defined
"x = 2" binds the name 'x' in foo's local namespace to the object '2'. For this, it doesn't matter whether the name 'x' was previously bound to anything. "x[2] = 2" is a shorthand notation for the method call "x.__setitem__(2,2)". This requires the name 'x' to be bound to some object that has a __setitem__ method. -Carsten -- http://mail.python.org/mailman/listinfo/python-list