[Python] classic vs new-style Python classes
Bob Miller
kbob at jogger-egg.com
Fri Jun 6 22:35:45 PDT 2008
Rob Hudson wrote:
> Anyone know more about "new-style" classes?
>
> See this:
> http://jcalderone.livejournal.com/39678.html
>
> Should you or shouldn't you derive from `object`? Based on that post
> it seems buggy if you do, but then why is it the "new-style"?
Last question first. It's "new-style" because old-style classes
were implemented first. They were found wanting, so their
replacement was designed. In Python 3000, all classes are new-style.
My personal strategy is to use old-style classes until I need
a new-style feature.
The advantage of my strategy is that I don't clutter up the source
code with a bunch of "derived from object" declarations.
The disadvantage is that I occasionally have to track down bugs where
the property or super() call I just wrote isn't working because I
inserted it into a member of an old-style class hierarchy. Usually
these are immediately obvious, but not always.
I disagree with the 'blog post you linked. While there are some
subtle differences, they're rarely differences that you rely on. I
personally have never broken code by converting a class to new-style.
And I have a tendency to use too much magic in my code.
FWIW, note that you can implicitly create a new-style class by
setting its metaclass to <type> or a subclass of <type>.
>>> class C: __metaclass__ = type
...
>>> C.mro()
[<class '__main__.C'>, <type 'object'>]
>>>
--
Bob Miller K<bob>
kbob at jogger-egg.com
More information about the Python
mailing list