scopes? (is: I don't get it ...)

W Isaac Carroll icarroll at pobox.com
Thu May 29 15:17:16 EDT 2003


Axel Bock wrote:
> whew, yes, thanks all of you! this helps ... ! :-)
> (although i still find it confusing that it prints without even warning
> ... )

If you notice, in the second version of your original code

     t1 = []
     t2 = 0
     def testme():
         print t1
         print t2
         t2+=1

it didn't print t2. It printed t1 then threw an exception. Printing t2 
only worked when there was no assignment to t2 in the function. When you 
added the assignment statement, it changed t2 into a local variable 
everywhere in the function.

TTFN






More information about the Python-list mailing list