[Python-ideas] Proposal: Abolition of bare except clauses
Steven D'Aprano
steve at pearwood.info
Sat Apr 11 17:15:08 CEST 2015
On Sat, Apr 11, 2015 at 11:44:23PM +1000, Chris Angelico wrote:
> Every document I've ever seen has taken one of two attitudes toward
> old-style classes. Either they should be considered deprecated, and
> every class you define should explicitly subclass something; or
> they're a really cool micro-optimization with a few fancy
> flexibilities that new-style classes don't have, but which come up
> only in the most obscure cases.
I wouldn't necessarily believe that classic classes are still faster
than new-style classes. I think it was certainly true back in the 2.2
and 2.3 days, but probably not in 2.7. In any case, I'd like to see the
benchmarks demonstrating a speed advantage to classic classes before
believing that they are an optimization in practice.
As far as I can see, the only things classic classes can do that
new-style classes don't are:
(1) Per-instance overriding of dunder methods on the instance. (I'm sure
there's a Design Pattern name for this, but I can never remember it.)
You can override regular methods for a particular instance by giving it
an instance attribute that is a bound method:
class Parrot:
def speak(self): return "Want a cracker!"
polly = Parrot()
polly.speak = types.MethodType(lambda self: "Who's a pretty boy?", polly)
but this doesn't work with dunders in new-style classes. In
classic classes, it does.
(2) Automatic delegation including dunder methods.
Both of these may be important, but if you are migrating to 3.x you are
going to lose them anyway, so you have to deal with it somehow.
(Probably by crying a lot.)
--
Steve
More information about the Python-ideas
mailing list