473,781 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1446
On May 29, 10:07 am, allendow...@gma il.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...@gma il.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
documentatio n
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.htm l>

but it seems right.

Thank you,

Alan Isaac


Jun 27 '08 #4
Alan Isaac <ai****@america n.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.htm l>

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.ClassTyp e) is
used.

Look at the third point.

--
Arnaud
Jun 27 '08 #5
On May 29, 12:07 pm, allendow...@gma il.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...@gma il.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*********@gma il.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.comwrite s:
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2026
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 (#46, Jul 29 2003, 18:54:32) on win32 >>> class OldException: pass >>> raise OldException()
1
1839
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 python class that was inheriting from the new builting 'object' class like so: class MyClass(object): The problem is in detecting that this class should be treated as a class
12
3833
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={}): class C(object): pass C.__bases__ = bases dict = 0
4
2083
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 of new-style classes apparently don't return true for PyInstance_Check, which causes a problem in PySequence_Check, since it will only do an attribute lookup for instances. Things probably shouldn't be this way. Should I go to python-dev with...
12
1593
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 please enlighten me? Thanks!
3
1281
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, the reason for new style classes are to make the whole type/object thing work better together. There are a number of new features etc. I think my problem is when new-style classes are used, at first I thought that all classes become new-style...
1
1444
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
1950
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 (again: compact ones) which describe unification attemps subjecting * New Style Classes
4
1260
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): or
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9939
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4040
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.