[Tutor] Subclassing vs. stand alone functions

chrispython at mac.com chrispython at mac.com
Sat Jun 16 16:46:24 CEST 2007


Thanks again, this is exactly the kind of info I need to make the jump from 
procedural to OO design.   I bookmarked your site for reference.
On Saturday, June 16, 2007, at 09:30AM, "Kent Johnson" <kent37 at tds.net> wrote:
>chrispython at mac.com wrote:
>> Hi there,
>> 
>> I am new to Python and trying to get my head around the OO stuff. I
>> guess my question is - when do you go with subclassing vs. making a
>> standalone function?
>
>> Let's say you want to load a dictionary. Do I create a function that
>> accepts some argument (say a file name) and returns a dictionary, or
>> do I subclass dict and override the __init__  and __setitem__
>> functions to make 'self-loading' dictionary? It seems the end result
>> is the same.
>
>I tend to reserve OOP for the cases where it provides a clear benefit 
>and use a procedural style otherwise. So in this case I would make a 
>separate factory function to load the dictionary because it is simpler. 
>Also you are not changing the behaviour of your new dict so overriding 
>seems unnecessary. (BTW Why would you override __setitem__?)
>
>There are many cases where OOP has a clear benefit in simper code, 
>encapsulation or reusability. There are times when you have to use OOP, 
>for example when using a library that is specialized by subclassing, 
>such as a GUI library or the cmd package.
>
>There are also many cases where OOP just adds unneeded complication to 
>your code; in these cases Python lets you write in the simpler 
>procedural style.
>
>I have written more on this topic here:
>http://personalpages.tds.net/~kent37/stories/00014.html
>
>Kent
>
>


More information about the Tutor mailing list