Named code blockes

D-Man dsh8290 at rit.edu
Tue Apr 24 13:51:27 EDT 2001


On Tue, Apr 24, 2001 at 06:05:30PM +0200, Alex Martelli wrote:
| "D-Man" <dsh8290 at rit.edu> wrote in message
| news:mailman.988076385.11192.python-list at python.org...
| > On Mon, Apr 23, 2001 at 04:49:01PM -0700, James_Althoff at i2.com wrote:
| > |
| > | <jim response>
| > |
| > | In Java unnamed (anonymous, inner) classes
| > | are used all over the place in Swing-GUI code.
| > | Not advocating -- just pointing it out.  :-)
| > |
| > | </jim response>
| >
| > IMO that is a kludge to work-around for a significant language
| > deficiency -- if functions (and classes) were first-class objects the
| > function object itself could be passed as the event handler!
| 
| It seems that when one CAN does something, one DOES do it:-).
| 
| I have never agreed with the Borland/Microsoft position that
| the lack of first-class callables is a "huge gaping hole" in
| Java (the same excellent software architect who did Delphi
| for Borland then moved to Microsoft to do similar frameworks
| for them -- first for Java enriched with first class callables
| called "delegates", then for C# who also has such constructs --
| may be responsible for both firms' "position" in this:-).  I

Interesting history.

I think some of your|my opinion might have to do with background.  I
did my first GUI work using gtk--, a C++ wrapper around GTK which is
written in C.  I would have a window class something like :

class Window : public Gtk::Window
{
    public void on_button1_relased( Gtk::ButtonEvent event )
    {
        // do something with the window, possibly enabling/disabling
        // other buttons
    }

    public void on_button2_relased( Gtk::ButtonEvent event )
    {
        // do something with the window, possibly enabling/disabling
        // other buttons
    }

    public Window()
    {
        button1->add_callback( &Window::on_button1_released ) ;
        button2->add_callback( &Window::on_button2_released ) ;
    }
}

(it has been a while since that project, so I don't remember all the
names exactly)


When I started working with Swing I thought it was really bad design
to have _all_ (button) events go through a single method
"actionPerformed", then that method would be like

    if ( is button1 )
    {
        on_button1_released()
    }
    else if ( is button2 )
    {
        on_button2_released()
    }


Then I learned that instances of inner classes have access to the
outer class's instance's private data.  (no breaking encapsulation
here, right <wink>).

I think the registering of a callback is a nicer way of achieving the
results, but that is just my preference.

-D





More information about the Python-list mailing list