Steven D'Aprano a écrit : > On Mon, 19 Mar 2007 19:48:37 +1100, Ben Finney wrote: > >> It's also best to inherit every class from another class, leading to a >> single hierarchy for all classes and types. 'object' is the one to >> choose if you don't want the behaviour of any other class. > > What's wrong with old-style classes?
Almost everything. That's why Python 2.2 introduced new-style classes. IIRC it was some 6 years ago now. > On the plus side: > > - Why inherit from something if you don't need to? You don't have to inherit. You can also make a class new-style by setting it's __metaclass__ attribute properly. But that's more typing !-) > - Less typing. Lol. Six keystrokes in the worst case. > - Attribute-lookup is much faster, perhaps as much as twice as fast. > http://www.python.org/~jeremy/weblog/030506.html This was in 2003. Did you bother testing now ? with Python 2.4.4: >>> class Old: ... def __init__(self): ... self.attr = "old" ... >>> class New(object): ... def __init__(self): ... self.attr = "new" ... >>> from timeit import Timer >>> told = Timer('old.attr', 'from __main__ import Old; old=Old()') >>> tnew = Timer('new.attr', 'from __main__ import New; new=New()') >>> told.repeat() [0.40867519378662109, 0.39075493812561035, 0.38998913764953613] >>> tnew.repeat() [0.58840394020080566, 0.5948030948638916, 0.36941695213317871] Not really "twice as fast" AFAICT. Now if you're really favor raw speed over expressivity, then you might wan to choose another language. > > - Documentation on old style classes is more extensive. Since new-style classes are backward compatible with old-style ones, almost all the old-style classes documentation applies to new-style ones as well. Everything else is quite well documented too: http://www.python.org/doc/newstyle/ > - You can't use new style classes for exceptions. Exceptions are new-style classes in 2.5. > > > On the minus side: > > - Properties don't work as you expect them too. properties don't work. Period. Properties rely on the descriptor protocol, which only works with new-style classes. > - Slots don't work at all. > - no support for the descriptor protocol - no __getattribute__ - no metaclasses - no proper constructor - no classmethod - no super() > In other words, the only reason why you HAVE to use a new style class is > that you need properties or __slots__. The reason why you have to use new-style classes is that it's the official Python object model since december 2001 - old-style classes being kept for compatibility. The reason why you want to use them is that they provide everything old-style classes did, and *way much more*. FWIW, if Python didn't have this powerful object model, I would have switched to another language long ago. And I'm probably not the only one. > You might WANT to use a new style > class to inherit from built-in types. Since builtin types are all new-style classes, any class inheriting from a builtin is a new-style class. FWIW, that's one of the reason for the object model changes way back in 2001. > Otherwise, the choice between old > and new is not very important. Your opinion. Too bad you're missing some of the most powerful parts of the language. -- http://mail.python.org/mailman/listinfo/python-list