should I put old or new style classes in my book?

Jason tenax.raccoon at gmail.com
Thu May 29 14:06:50 EDT 2008


On May 29, 10:07 am, allendow... at gmail.com wrote:
> Hi All,
>
> I am working on a revised edition of How To Think Like a Computer
> Scientist,
> which is going to be called Think Python.  It will be published by
> Cambridge
> University Press, but there will still be a free version under the GNU
> FDL.
>
> You can see the latest version at thinkpython.com; I am revising now,
> so
> I welcome all comments, suggestions, corrections, etc.
>
> Anyway, I am posting to ask about the current status of new style
> classes.
> I am planning to present only one style in the book, because the
> differences
> between them don't matter for anything I am doing in the book.
>
> The current edition of the book presents old style classes.  I am
> considering
> switching to new style classes on the assumption that this should be
> the default
> choice for new programs.  The drawback is that a lot of the online
> documentation
> still uses old style classes.
>
> Thanks for any guidance you can provide.
>
> Cheers,
> Allen

I've got Python 3.0 alpha 2.  In this version, it looks like you can
define classes in either the old style or new style.  (I snipped the
top line a bit in the following example):

Python 3.0a2 (r30a2:59405M, Dec  7 2007, 15:23:28
Type "help", "copyright", "credits" or "license"
>>> class one(object): pass
...
>>> class two: pass
...
>>> two
<class '__main__.two'>
>>> one
<class '__main__.one'>
>>> type(one)
<type 'type'>
>>> type(two)
<type 'type'>
>>>

That said, old-style classes can't use the staticmethod or classmethod
properties correctly.  New-style classes better support multiple
inheritance and old-style classes can't use metaclasses.  Metaclasses,
and even multiple inheritance may be beyond the scope of your book,
though.

I'd recommend new-style classes myself, as it avoids some nasty subtle
problems if your readers move to more advanced techniques.  You are
correct, though, that some of the classes in the Python library use
the old style.

So, you might want to acknowledge that there are two ways of doing
classes, that a lot of old code uses the old way.  The new way adds
some powerful new techniques that may be beyond the scope of your
book.  In Python 3.0, it won't matter, as everything is a new-style
class anyway.

  --Jason



More information about the Python-list mailing list