[Python-Dev] 2.5 and beyond

Bill Chiles billchi at microsoft.com
Tue Jul 4 01:17:36 CEST 2006


For Common Lispers and probably Schemers, Python has some surprising
semantics around scope and lifetime extent of variables.  Three that
leap out at me are:
 * function parameters with default values are NOT new bindings for each
invocation, so a
   default value of [] changes if you destructively modify this list
object in the function
 * loop variables are NOT distinct lexical variables.  The binding gloms
on to a variable in the
   function's scope, both changing that lexical binding and not creating
a new one for the
   loop (that goes away when the loop's scope ends)
 * loop variables are NOT distinct bindings per iteration, leading to
the surprising results
   below

Bill

-----Original Message-----
From: python-dev-bounces+billchi=microsoft.com at python.org
[mailto:python-dev-bounces+billchi=microsoft.com at python.org] On Behalf
Of Bill Janssen
Sent: Friday, June 30, 2006 6:31 PM
To: Giovanni Bajo
Cc: Phillip J. Eby; Ka-Ping Yee; Guido van Rossum; Tim Peters;
python-dev at python.org
Subject: Re: [Python-Dev] 2.5 and beyond

> >>> a = []
> >>> for i in range(10):
> ...     a.append(lambda: i)
> ...
> >>> print [x() for x in a]
> [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]

Isn't this exactly what you'd expect?  Maybe I've been writing Python
for too long... :-).

Bill
_______________________________________________
Python-Dev mailing list
Python-Dev at python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/billchi%40microsoft.co
m


More information about the Python-Dev mailing list