general class functions

Dan Perl danperl at rogers.com
Wed Nov 3 13:59:21 EST 2004


I probably cannot do justice to the topic, although I have personally 
encountered cases where by following the advice of people more experienced 
in python than me I was able to improve my code.  But I can point you to a 
couple of links:
http://www.python.org/2.2.1/descrintro.html
http://www.python.org/doc/2.2.1/whatsnew/

In a nutshell, subclassing from object is part of the new-style classes 
introduced in python 2.2.  Quoting from "Learning Python" 2nd ed., "perhaps 
the most visible change in new style classes is their slightly different 
treatment of inheritance for the so-called diamond pattern of multiple 
inheritance trees-where more than one superclass leads to the same higher 
superclass further above".  Look also at cooperative methods in the links I 
mentioned above (if I remember correctly, cooperative methods have been the 
main reason for me to change classes to the new style).

I'm sure other people in this newsgroup can make a better case for new-style 
classes than I can.

HTH,

Dan

"syd" <syd.diamond at gmail.com> wrote in message 
news:a76ba315.0411030818.9457fe3 at posting.google.com...
> "Dan Perl" <danperl at rogers.com> wrote in message
>> >>>> library.get_continent('Europe')
>> This line creates and returns an instance of Library.  I assume that you
>> imported foo in a python session and then you executed that line.  That
>> results in a call to Library.__repr__( ), which is not defined.  First of
>> all, I recommend you change class Library to subclass from object. 
>> That's
>> just good practice in general and in this case it will give you a default
>> __repr__( ) method.  But that's probably not what you want.  What you
>> probably want is to still use a line like:
>>     print library.getContinent('Europe').getNameList()
>> Otherwise, library.get_continent('Europe') is going to print something 
>> like:
>>     <__main__.Library object at 0x00A9F430>
>
> You're 100% right, Dan -- I meant to have the "getNameList()" tacked
> on.
>
> But more importantly, why is subclassing object a good practice in
> general?  I see under dir(object) that you get some good default
> methods.  I looked under the documentation, but could not find a good
> specific examples as to why this was a good practice.  Can you
> elaborate, please? 





More information about the Python-list mailing list