Why I chose Python over Ruby

Terry Reedy tjreedy at udel.edu
Sun Mar 5 19:36:12 EST 2006


"Schüle Daniel" <uval at rz.uni-karlsruhe.de> wrote in message 
news:dufm2r$p89$1 at news2.rz.uni-karlsruhe.de...
>> block (Python forbids the rebinding of variables coming from an
>> enclosing but non-global scope, to avoid facing this issue).
>
> I am not sure what you mean here
> can you elaborate on this please
>
> >>> def a():
> ...     q = []
> ...     def b(x):
> ...             def c(y):
> ...                     def d(z):
> ...                             q.append(x)
> ...                             q.append(y)
> ...                             q.append(z)
> ...                     d(1)
> ...             c(2)
> ...     b(3)
> ...     return q
> ...
> >>> a()
> [3, 2, 1]

You are mutating q, not rebinding it.  As another poster discovered, 
replacing the appends by the nominally equivalent augmented assignments:

q += [x] #etc

does not work.  Since the compiler does not know the type of q, it assumes 
that it needs to be rebound and compiles code to do so, even though here is 
would be to the same object.  That makes q an (uninitialized) local.

Terry Jan Reedy







More information about the Python-list mailing list