i++ in Python?

OtisUsenet otis_usenet at yahoo.com
Thu Jul 18 22:55:38 EDT 2002


Peter Hansen <peter at engcorp.com> wrote in message news:<3D37429C.53BC0FDB at engcorp.com>...
> OtisUsenet wrote:
> > 
> > Python newbie question.  How does one do something like this in
> > Python:
> > 
> >     # assign the value of j to i and then increment j by 1
> >     i = j++
> > 
> > I checked the FAQ at http://www.python.org/doc/FAQ.html, but this
> > simple question wasn't covered :)  I can't find it in 2 Python books
> > either.
> 
> Is there anything you don't like about doing it exactly as your comment
> reads?

Yes, actually :)
I don't like the fact that if 'j' is some kind of a global counter
variable, and I'm dealing with multiple threads, it is then, I
believe, possible for 2 or more threads to execute i = j at the same
time (i.e. before 'j' had the chance to increase).
One can probably work around this....
...although, now that I think about it, even i = j++ wouldn't be
thread-safe completely, as on the machine code level this still gets
executed in multiple steps.....I think. :)

> >>> i = j
> >>> j = j + 1
> 
> On newer Pythons, you can replace the second line with j += 1 if you wish.

Thanks,
Otis



More information about the Python-list mailing list