Python exceptions and refcounts
Alex Martelli
aleax at aleax.it
Wed Jan 23 08:39:42 EST 2002
"Kragen Sitaker" <kragen at pobox.com> wrote in message
news:83y9ip4deh.fsf at panacea.canonical.org...
...
> Lately I've been doing this kind of thing; instead of writing:
>
> def getnext(self):
> tmp = self.seq[self.ii]
> self.ii = self.ii + 1
> return tmp
>
> I do this:
> def getnext(self):
> try: return self.seq[self.ii]
> 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 ...
Alex
More information about the Python-list
mailing list