Python exceptions and refcounts

Alex Martelli aleax at aleax.it
Thu Jan 24 06:10:24 EST 2002


"Kragen Sitaker" <kragen at pobox.com> wrote in message
news:83sn8w4u71.fsf at panacea.canonical.org...
> "Alex Martelli" <aleax at aleax.it> writes:
> > "Kragen Sitaker" <kragen at pobox.com> wrote in message
> > news:83y9ip4deh.fsf at panacea.canonical.org...
> > >     finally: self.ii = self.ii + 1
> > >
> > > In the absence of exceptions, both will do the same thing.
> > >
> > > I'm not sure this is an actual improvement; I think I may be
> > > succumbing to the Premature Optimization Demon, in its worst form: the
> > > tendency to shorten one's code.
> >
> > Naah -- if shortening was obsessing you, you'd code the
> > finally clause's body as self.ii += 1 ...
>
> += is unpythonic (it confuses the hell out of new Python programmers,
> it's More Than One Way To Do It, it's implicit rather than explicit,
> and its raison d'etre is performance optimization, and all this has no
> doubt been said before, so I'll stop now) and isn't portable to
> pre-2.0 versions of Python, so I don't use it very often.

See?  You aren't obsessed with shortening your code -- you're obsessed
with completely different concerns.  So, you're not succumbing to the
specific demon you were thinking about (typical: it's those you DON'T
think about, that get you...:-).

Unfortunately, we _already_ had MTOWTDI for "increment ii by 1", e.g.:
    ii = ii + 1
    ii = 1 + ii
to name but two pre-existing WTDI's.  Pending the World's Mathematical
Council's decision on my petition to abolish integer addition's
commutative property (and unfortunately I face much opposition
there, by an unholy coalition of "if it was good enough for Gauss
it's good enough for me" conservatives and outright MTOWTDIsts),
we'll have to suffer with it...

[P.S.: another problem is that "ii += ..." is THE explicit way to
express "rebind if immutable to previous value altered by adding",
while "ii = ii + ..." expresses it _implicitly_ due to the
happenstance of rebind-target and augend identity...]


Alex






More information about the Python-list mailing list