Tuple assignment and generators?
jemfinch at gmail.com
jemfinch at gmail.com
Fri May 5 02:08:09 CEST 2006
Larry Bates wrote:
> Just wrote:
> > In article <0KydnWcEJaVCtcfZnZ2dnUVZ_tmdnZ2d at comcast.com>,
> > Larry Bates <larry.bates at websafe.com> wrote:
> >> While I have never needed anything like this in my 5 years of Python
> >> programming, here is a way:
> >> a,b,c = 3*
> >> q,r,s,t,u,v = 6*
> > This is (IMO) fairly idiomatic:
> > a = b = c = 0
> > q = r = s = t = u = v = 0
> > Just
> You must be careful with this as they all point to
> exactly the same object. Example:
> >>> q = r = s = t = u = v = 0
> >>> id(q)
> >>> id(r)
> >>> id(s)
> Notice that all of them point to exactly the same object,
> not 6 copies of zero which is "probably" what the poster
> was thinking.
Numbers are immutable. They're never copied. Zero, in particular, is
the same variable all throughout a Python interpreter.
> Most of the time when I see this, it is because people are
> thinking of variables having values which is mostly a
> carry-over from old Fortran/Cobol/Basic programming ideas.
Most of the time when I see it, it's written by someone who's used
Python for quite some time. It's a standard Python idiom. You'll find
it all over the standard library. It's not a carry-over from
Fortran/Cobol/Basic at all.
> In python variables are pointers to objects. Objects could
> be values, but they are not placeholders where you store
And all immutable objects are indistinguishable from values. Immutable
objects include ints, longs, strings, unicode objects, tuples,
frozensets, and perhaps some others that I'm forgetting.
> I read on this list (quite frequently) that people
> think they are getting 6 separate variables each with
> a zero stored in them.
That's because they are. They're getting 6 different pointers
(bindings) to zero. If you change one, the others remain pointed at
(bound to) zero.
> They are not. They are getting
> six pointers that all point to an integer zero
Six *different* pointers. Six *different* bindings.
> (IMHO it
> would be a rather odd application for a programmer
> to want this).
No, it wouldn't be. It's exactly how a person works with immutable
> Here is where multiple assignments
> causes problems for beginners:
> >>> a=
> >>> b=c=a
> >>> a.append(6)
> >>> b
Yes, that does sometimes trouble beginners to programming. But so do
regular expressions. Programmers ought not to restrict themselves by
what beginners will have no difficulty learning.
> What?? 'b' should contain an empty list, right? Nope.
> a, b, and c all point to the SAME list just like the
> poster's q, r, s, t, u, v all point to the SAME zero.
There is only one zero in Python! It can never change!
> What they meant to write was:
> c=a[:] # Shallow copy of list
> My rule, don't do it unless you know exactly why you
> want to do it. It will trip you up at some point and
> be VERY hard to find.
Your rule should only be followed by people who obstinately refuse
either to understand the way variable bindings work in Python, or by
people who refuse to know or care whether a given kind of object is
immutable. And that group of people doesn't seem to include any of the
good Python programmers I know.
More information about the Python-list