Curious assignment behaviour

Paul Rubin phr-n2001d at nightsong.com
Wed Oct 10 03:29:19 EDT 2001


"Tim Peters" <tim.one at home.com> writes:

> [Paul Rubin, on an expression assignment operator]
> > Why on earth does it have to be different from the statement assignment
> > operator?  What's wrong with
> >
> >   while x=get_next(): whatever(x)
> >
> > I just don't buy the rationale that using = instead of == is a big source
> > of bugs.  In 25 years of hacking C code, I think I've seen that error
> > maybe once or twice.
> 
> I suggest you're highly atypical here, Paul.  This was the very first of the
> gotchas listed in Andrew Koenig's classic (late 80s) "C Traps and Pitfalls",
> and has made nearly every list of C "deadly sins" I've seen since then.
> I've wasted weeks of my own life helping people track this error down in C,
> too often under extreme time or customer pressure.  The experience of the
> other folks at PythonLabs is similar, so even if it's a disease you're
> immune to, you can be sure this will never go in.

The thing is, it's just not a matter of MY immunities.  I've spent
plenty of time debugging other people's code too, and have encountered
all kinds of errors in both their code and mine.  The =/== mistake has
just been very rare in my experience.  Can it be that the Unix
compilers I use flag the error a lot of the time, so it gets fixed
quickly, while Windows compilers don't flag it, and you're mostly
using Windows compilers?

Anyway, I can live with "while x:=get_next()" instead of x=get_next.
But I feel seriously cramped in Python when I have to say

  while 1:
     x=get_next()
     if not x: break
     whatever(x)

so I hope something is done about the issue.

> Oops!  The assert turned out to be the perversely self-fulfilling:
> 
>     assert(i = 1);

Ouch!!!! ;-)



More information about the Python-list mailing list