The Artima Developer Community
Sponsored Link

Python Buzz Forum
Proper OOP

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Patrick Lioi

Posts: 45
Nickname: plioi
Registered: Aug, 2003

Patrick Lioi is a software developer in Austin, Tx.
Proper OOP Posted: Aug 20, 2003 3:38 AM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Patrick Lioi.
Original Post: Proper OOP
Feed Title: Patrick Lioi on Python
Feed URL: http://patrick.lioi.net/syndicate/Python/RSS2.xml
Feed Description: Entries from the Python category of my personal site.
Latest Python Buzz Posts
Latest Python Buzz Posts by Patrick Lioi
Latest Posts From Patrick Lioi on Python

Advertisement

There are strong arguments favoring inclusion over inheritance in object oriented programming languages. Inheritance breaks encapsulation. You often have to know the internals of an object to subclass it properly. Even when done with relative care, it is not very difficult to produce spaghetti code when subclassing. Side effects abound. Having to keep track of implementation details at all levels of a hierarchy can be overly complicated, which can be disastrous if changes need to be made in the middle of the hierarchy.

So, we should favor inclusion over inheritance as much as possible. This way, you keep your abstractions in order by keeping details hidden behind your interfaces. Your interfaces are easier to memorize than the code that they hide. As long as the maintainer of an object keeps his interface intact, your including-code works just like you want it to. (If the maintainer breaks his interface, everybody is screwed regardless of inheritance-vs-inclusion.)

Despite this argument, we can't just go whole hog with inclusion. That would lead to reinventing the wheel far too often. So, what's the best way to compromise? I'd say that python encourages doing things the Right Way, without necessarily forcing it upon you. By allowing multiple classes to appear in the same module -- that is, in a single source file -- the programmer is encouraged to use "small scale inheritance", within the scope of the particular module he wishes to expose to the rest of the world. The person(s) responsible for a single source file are the ones fit to know about implementation details, and are fit to deal with avoiding side effects. The outside world can then use those objects via inclusion without worry. Java fails here in that placing multiple public classes in the same package means placing them in separate source files. Python's organization has a gentle psychological effect on the programmer, encouraging this small scale inheritance within a domain that he has already determined to be a cohesive unit. His goal is to make that unit, and all the classes that make it up are apt to be "aware" of each others' guts.

But there are still times when it's a good idea to subclass across modules. UserDict is a __builtin__ class that lets python programmers extend the built-in dictionary class. If your subclasses don't make assumptions about the internals of parent classes, inheritance-across-modules lets you do some pretty elegant things with little effort. For example, if there's a SAX parsing class that lets you define event-handling methods, it can be more elegant to do so with inheritance than the alternatives.

So, there seem to be special cases in which inheritance-across-modules is not only allowed, but encouraged. What these special cases have in common is that the obvious intended use of subclassing is inherent to the API of the parent class. The parent class interface is clearly specified for the purpose of subclassing, the parent class interface is small, and the methods meant to be overridden have a clear purpose in the act of subclassing. Overriding SAX event handlers makes sense because that is what they are there for in the API.

Subclassing across modules just doesn't work for more arbitrary situations, where the goal of the particular subclassing falls outside the scope of the obvious intended use inherent in the parent API.

To sum up:

  1. Actively subclass on a small scale, within modules, to benefit from code reuse.
  2. Favor inclusion across modules to avoid spaghetti code.
  3. Subclass across modules only when the parent class was designed to be subclassed across modules.
  4. Design such parent classes to have clear and small API's, where the intended purpose of subclassing is inherent in the API.

Read: Proper OOP

Topic: So you want to be a... Previous Topic   Next Topic Topic: CSV module ranting...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use