Re: Tuple assignment and generators?




Larry Bates wrote:
Just wrote:
In article <0KydnWcEJaVCtcfZnZ2dnUVZ_tmdnZ2d@xxxxxxxxxxx>,
Larry Bates <larry.bates@xxxxxxxxxxx> wrote:

While I have never needed anything like this in my 5 years of Python
programming, here is a way:

a,b,c = 3*[0]
q,r,s,t,u,v = 6*[0]

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)
3301924
id(r)
3301924
id(s)
3301924


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
stuff.

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
(value) objects.

Here is where multiple assignments
causes problems for beginners:

a=[]
b=c=a
a.append(6)
b
[6]

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
b=a[:]

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.

Jeremy

.



Relevant Pages

  • Re: tough-to-explain Python
    ... How do I explain to rank beginners (no programming experience at ... immutable is a key to getting up-to-speed quickly with Python as well ... i can tag objects anytime, tag objects more than once, remove ...
    (comp.lang.python)
  • Re: My Python annoyances
    ... Everyone know that programming is supposed to be a dark art, ... Computer code is supposed to be something ... Python takes all of that away. ... To make it even friendlier to beginners, ...
    (comp.lang.python)
  • Re: My Python annoyances
    ... Everyone know that programming is supposed to be a dark art, ... Computer code is supposed to be something ... Python takes all of that away. ... To make it even friendlier to beginners, ...
    (comp.lang.python)
  • My Python annoyances
    ... Everyone know that programming is supposed to be a dark art, ... Python takes all of that away. ... To make it even friendlier to beginners, ... full of arcane stuff and, thankfully, requiring typing declaration. ...
    (comp.lang.python)
  • Re: Customize the effect of enumerate()?
    ... you expecting that will be neither less than zero, greater than zero, ... I don't like the error message. ... tell the caller what went wrong and why it is an invalid index. ... programming that defensively, I'd write: ...
    (comp.lang.python)