How to write this statement more compactly without a ++ operator?
<p>Here's what I want to write:</p>

<pre><code>groups[m][n] = groups[m - 1][n] or ++gid
</code></pre>

<p>Here's what I have to write:</p>

<pre><code>g = groups[m - 1][n]
if g:
    groups[m,n] = g
else:
    gid += 1
    groups[m][n] = gid
</code></pre>

<p>Is there no more compact way of writing that in Python simply because it lacks a <code>++</code> operator?</p>

<hr>

<p>A larger sample from a method I'm working on:</p>

<pre><code>groups = [[0] * self.columns] * self.rows
gid = 0
for m in xrange(self.rows):
    for n in xrange(self.columns):
        stone = self[m, n]
        if stone == self[m - 1, n]:
            if groups[m - 1][n]:
                groups[m][n] = groups[m - 1][n]
            else:
                gid += 1
                groups[m][n] = gid
        elif stone == self[m, n - 1]:
            if groups[m][n - 1]:
                groups[m][n] = groups[m][n - 1]
            else:
                gid += 1
                groups[m][n] = gid
</code></pre>

<p>I think it's a lot harder to read when I have to blow it out like that, plus I'm evaluating <code>m-1</code> twice... I'm not sure how I can condense it though.</p>

<hr>

<p>This is what I came up with:</p>

<p>I created a wrapper class around <code>int</code>:</p>

<pre><code>class Int(object):
    def __init__(self, i):
        self.i = i

    def pre(self, a=1):
        self.i += a
        return Int(self.i)

    def post(self, a=1):
        cpy = Int(self.i)
        self.i += a
        return cpy

    def __repr__(self):
        return str(self.i)

    def __nonzero__(self):
        return self.i != 0
</code></pre>

<p>Which can be used like this:</p>

<pre><code>def group_stones(self):
    groups = [[None for _ in xrange(self.cols)] for _ in xrange(self.rows)]
    gid = Int(0)
    for m in xrange(self.rows):
        for n in xrange(self.cols):
            stone = self[m, n]
            if stone == self[m - 1, n]:
                groups[m][n] = groups[m - 1][n] or gid.pre()
            elif stone == self[m, n - 1]:
                groups[m][n] = groups[m][n - 1] or gid.pre()
            else:
                groups[m][n] = gid.pre()
</code></pre>

<p>Much like I would do in other languages.</p>
