473,288 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,288 software developers and data experts.

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

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
11 1403
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
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
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
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
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
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Andrew | last post by:
Hi, To satisfy my curiosity I was wondering if anyone knew if this behaviour was intentional? Is there a specific reason why exceptions are not allowed to be new style classes? Python 2.3...
1
by: Eric Wilhelm | last post by:
Hi, By new-style classes, I'm referring to the changes which came into 2.2 as a result of PEP 252 and 253. I discovered this problem when trying to use the Perl Inline::Python module with a...
12
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}):...
4
by: Tuure Laurinolli | last post by:
Someone pasted the original version of the following code snippet on #python today. I started investigating why the new-style class didn't work as expected, and found that at least some instances...
12
by: Vaibhav | last post by:
I recently heard about 'new-style classes'. I am very sorry if this sounds like a newbie question, but what are they? I checked the Python Manual but did not find anything conclusive. Could someone...
3
by: Kalle Anke | last post by:
I'm confused by the concepts of old-style vs new-style classes, I've read most of the documents I found about this but it doesn't "click". Probably because I wasn't around before 2.2. Anyway,...
1
by: ankit | last post by:
Hello, Please put some light on, What are new style classes and classic style classes in python. The basic differences in them. And How can I decide to choose one.
12
by: Ilias Lazaridis | last post by:
Another topic has raised the need of a deeper teach-in. Where can I find _compact_ documentation about * Differece between New Style / Old Style Classes Are there any documents available...
4
by: Isaac Rodriguez | last post by:
Hi, This is probably a very basic question, but I've been playing with new style classes, and I cannot see any difference in behavior when a declare a class as: class NewStyleClass(object): ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.