[Tutor] communication between class instances

Magnus Lycka magnus@thinkware.se
Thu Dec 5 14:08:02 2002


At 23:00 2002-12-04 -0700, Poor Yorick wrote:
>I've been studying Python for several months now, and it is my first 
>attempt to learn how to program in any language.  I've been trying lately 
>to understand some of the nuances of object oriented programming, but most 
>of the resources on the web require some knowledge of c++ to gist.

Unfortunately it seems OO isn't so easy to grasp. I'm not
sure whether this is due to some intrinsic complexity of
the OO concept, due to poor tutorials, poor support in the
programming languages or that people are taught to think in
ways that contradict or collide with OOP...

Whatever the reason, most programmers in so-called OOP languages
such as Java and C++ don't really understand Object-Oriented
thinking. :(

>  So, I'm turning to this list :)

Just the right thing to do. ;)

>Direct answers or pointers to reference material about this question would 
>be greatly appreciated:

I like Arthur Riel's book Object-Oriented Design Heuristics, but
it's probably not the best fit for a beginner to programming. :(

There is a lot of .py-files in your python distribution that
shows you how classes might look.

>Say I have the following classes:
>
>class app:
>    def __init(self):
>        self.dbtype = None
>        self.gui=gui(self)

I'm not sure what you mean that "app" should be but I try to
make sure that my program logic is unaware of the GUI.

>class gui:
>    def __init__(self, app):
>        self.app = app
>        self.main_menu = menu(self.app)
>
>class menu:
>    def __init__(self, app):
>        self.app = app
>
>    def dbtype(self):
>        app.dbtype = 'metakit'

It seem to me that this code doesn't actually do anything useful.
It's difficult to say a lot based on code that doesn't do anything
useful. I've never seen a Python program with a class called gui.
I don't know what that would stand for.

>So far, instantiating classes and passing references to all other class 
>instances that the instance will need to communicate with is the only way 
>I've come up with to get class instances to communicate with each 
>other.  This seems unwieldy and not really modular.

The Law of Demeter says:

       Do not refer to a class C in a method m unless C is the type of
           o an instance variable in m's class definition,
           o an argument of m,
           o an object created in m,
           o a global variable.

Lieberherr, K.J. and I.M. Holland, Assuring Good Style for Object-Oriented 
Programs. IEEE Software, 1989(September): p. 38-48.

>Is there a better way to pass data between class instances?  One thing I 
>really don't like is having to pass a reference to the app instance all 
>the way down to the menu instance, which really should only be aware of 
>the gui instance which instantiates it. Right?

Then the layer in between should have a method that shields
this I guess. But GUI programming is typically rather complex,
and usually depends on code in another language (Tcl/Tk or C++
for instance). It's probably not the best aspect of OO programming
to start with, even if it's a natural candidate for classes and
an object oriented approach.


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se