Bookmark and Share

Screen shot 2015-05-24 at 3.36.42 PMI like Python. A lot. But it has its limits, and short of forking a new version of Python for myself, sometimes it is just best to implement some kind of work-around. In this case, for pre- and post increment and decrement operations on counters, which Python regrettably lacks.

Coming from a c background as I do, this is something I really miss. The efficiency and clarity of this type of expression is very high. Some have described these operators as unclear (usually in the context of c itself), but that’s not really the case. Here’s how they work (in c.)

In c, one way to write a loop is like this…

i = 0;
while(i < 10)
{
    i = i + 1;
    do_something(i);
}

…that’s all well and good, but using pre-increment, which adds one to an integer at the same time it returns that integer’s value, we can just write…

i = 0;
while(i < 10)
{
    do_something(++i); // the first value passed out is one
}

…which is quite clear. as long as i is less than ten, do_something(). The value of i is passed to do_something() after being incremented — and so the name, “pre-increment.”

In c, post-increment is written i++ instead of ++i. The difference is, it gives you the value of i as it exists now, and then increments it. So the next time you use it, it will have increased by one:

i = 0;
while(i < 10)
{
    do_something(i++); // the first value passed out is zero
}

Decrement works the same way. −−i is pre-decrement, and i−− is post-decrement.

But in Python, you have to do these things more clumsily…


i = 0
while i < 10:
    i = i + 1       # (or "i += 1") either form is pre-increment, because...
    do_something(i) # ...the value is changed before it is used.

…in Python, to get pre- and post- operation, you have to move the line before or after do_something():


i = 0
while i < 10:
    do_something(i) # the first value passed out is zero.
    i = i + 1       # (or "i += 1") # this is post-increment

It’s not just a matter of a simpler syntax; it is also that when you’re writing the code, or looking at it later, it is more obvious, quicker and easier to comprehend. When you can use these operators, you’re looking right at the variable’s behavior when you look at where it is being used. Clarity!

So then, coming back around to what I’ve done here. I’ve provided an import library that provides increment and decrement, in both pre- and post- flavors. Using it is easy:


from aa_counter import *
i = counter() #default is to initialize to zero. Or just pass something else.
while i < 10:
    do_something(i.preinc()) # 1st value passed is one

Post-increment:

from aa_counter import *
i = counter() #default value is zero, or you pass something else
while i < 10:
    do_something(i.postinc()) # 1st value passed is zero

str(i) returns the integer as a string

So print i does also.

You can get directly at the value this way:

theValue = i.v

There are some other tricks in aa_counter.py, but the focus here is on getting a form of these specific operators into the Python code context.

Here’s the library, with built-in demos and examples.

aa_counter.py.gz

History:

Revision Changes
1.0.0 Initial Release.