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

A

allendowney

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
 
J

Jason

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"
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
 
A

Arnaud Delobelle

Jason said:
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"

Note that you can get the same behaviour in Python 2.2+ by setting the
global variable __metaclass__ to type:

marigold:~ arno$ python
Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 
A

Alan Isaac

This thread raises two questions for me.



1. I take it from this thread that in Python 3 the

following are equivalent:



class Test: pass



class Test(object): pass



Is that correct, and if so, where is it stated explicitly?

(I know about the "all classes are new style classes" statement.)



2. I take it from this thread that in Python 2.2+

if I put the following at the top of a module ::



__metaclass__ = type



then all the classes defined in that module will be newstyle

classes. Is that correct? Somehow I did not grok that from

<URL:http://docs.python.org/ref/metaclasses.html>

but it seems right.



Thank you,

Alan Isaac
 
A

Arnaud Delobelle

Alan Isaac said:
This thread raises two questions for me.

1. I take it from this thread that in Python 3 the following are
equivalent:

class Test: pass

class Test(object): pass

Is that correct, and if so, where is it stated explicitly?
(I know about the "all classes are new style classes" statement.)

I don't know where it is stated, but how could they *not* be
equivalent?
2. I take it from this thread that in Python 2.2+ if I put the
following at the top of a module ::

__metaclass__ = type

then all the classes defined in that module will be newstyle
classes. Is that correct? Somehow I did not grok that from

<URL:http://docs.python.org/ref/metaclasses.html>

but it seems right.

From the URL you quote:

The appropriate metaclass is determined by the following
precedence rules:

* If dict['__metaclass__'] exists, it is used.

* Otherwise, if there is at least one base class, its metaclass is
used (this looks for a __class__ attribute first and if not
found, uses its type).

* Otherwise, if a global variable named __metaclass__ exists, it
is used.

* Otherwise, the old-style, classic metaclass (types.ClassType) is
used.

Look at the third point.
 
C

Carl Banks

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.


New style, all the way.

The drawback you speak of for new-style classes I think is today more
of a drawback for old-style. The problem is, when a newbie goes
looking for examples, there is a lot of code out there that uses
things like properties, type(instance), @staticmethods, and so on.
Those won't work, and will confuse the hell out of newbies, if you
teach them old-style. OTOH, the examples out there that are written
for old-style usually still work for new-style classes.

The only significant issue, as far as I'm concerned, is the list of
bases. Which is why, even if you only cover new-style classes, it's
important to at least mention that there used to exist old-style that
don't list any bases (in Python 2.x). And then tell the user, "We're
not covering it, it's not something you need to worry about, and most
of the time you can and should add (object) and the code will still
work." And leave it at that--let the interested newbie seek out more
information on their own.


Carl Banks
 
S

sturlamolden

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.

You should use new-style classes. It is the default in Python 2.6 and
the only option in 3.0. These releases will be out before your book is
on the market.
 
B

Bruno Desthuilliers

(e-mail address removed) a écrit :
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.

Same remarks as anyone else that answered so far: definitively use
new-style classes, and just add a note about the old-style syntax.
 
E

Eduardo O. Padoan

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):

Wrong. Py3k Classes are always new-style. They subclass object
implicitly when no superclass is given.
Python 3.0a2 (r30a2:59405M, Dec 7 2007, 15:23:28
Type "help", "copyright", "credits" or "license"

Both classes are new style.
 
A

Arnaud Delobelle

Eduardo O. Padoan said:
Wrong. Py3k Classes are always new-style. They subclass object
implicitly when no superclass is given.

I think he was talking about syntax, not object types.
 
A

Aahz

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.

You've got a tough use-case. When is your book supposed to be done? To
what extent to you want to make your book work with 3.x?

Overall, I'm generally in favor of presenting both (I'm opposed to
new-style-only), but in your case it sounds like just new-style would be
better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top