By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jun 27 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
On May 29, 10:07 am, allendow...@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
Jun 27 '08 #2

P: n/a
Jason <te***********@gmail.comwrites:
On May 29, 10:07 am, allendow...@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'>
>>>>
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.
>>__metaclass__ = type
class Foo: pass
....
>>type(Foo)
<type 'type'>
>>>
--
Arnaud
Jun 27 '08 #3

P: n/a
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


Jun 27 '08 #4

P: n/a
Alan Isaac <ai****@american.eduwrites:
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.

--
Arnaud
Jun 27 '08 #5

P: n/a
On May 29, 12:07 pm, allendow...@gmail.com wrote:
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
Jun 27 '08 #6

P: n/a
On May 29, 6:07 pm, allendow...@gmail.com wrote:
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.

Jun 27 '08 #7

P: n/a
al*********@gmail.com 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.

Jun 27 '08 #8

P: n/a
On Thu, May 29, 2008 at 3:06 PM, Jason <te***********@gmail.comwrote:
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"
>>>class one(object): pass
...
>>>class two: pass
...
>>>two
<class '__main__.two'>
>>>one
<class '__main__.one'>
>>>type(one)
<type 'type'>
>>>type(two)
<type 'type'>
>>>>
Both classes are new style.
--
Eduardo de Oliveira Padoan
http://www.advogato.org/person/eopadoan/
http://twitter.com/edcrypt
Bookmarks: http://del.icio.us/edcrypt
Jun 27 '08 #9

P: n/a
"Eduardo O. Padoan" <ed************@gmail.comwrites:
On Thu, May 29, 2008 at 3:06 PM, Jason <te***********@gmail.comwrote:
>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.
I think he was talking about syntax, not object types.

--
Arnaud
Jun 27 '08 #10

P: n/a
In article <b9**********************************@c19g2000prf. googlegroups.com>,
<al*********@gmail.comwrote:
>
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.
--
Aahz (aa**@pythoncraft.com) <* http://www.pythoncraft.com/

Need a book? Use your library!
Jun 27 '08 #11

P: n/a
Alan Isaac <ai****@american.eduwrites:
>I take it from this thread that in Python 3 the following are
equivalent:
> class Test: pass
> class Test(object): pass

Arnaud Delobelle wrote:
I don't know where it is stated, but how could they *not* be
equivalent?
The most obvious way would be that the former became an illegal
syntax. But in Python 3 alpha, it is accepted, so I assume that it
will continue to be?

Cheers,
Alan Isaac
Jun 27 '08 #12

This discussion thread is closed

Replies have been disabled for this discussion.