Help me pick an API design (OO vs functional)

Michael Herrmann michael.herrmann at getautoma.com
Tue Mar 26 10:26:30 EDT 2013


On Tuesday, March 26, 2013 1:59:58 PM UTC+1, Steven D'Aprano wrote:
> On Tue, 26 Mar 2013 05:04:43 -0700, Michael Herrmann wrote:
> ...
> Am I the only one who appreciates the simplicity of
> 
> >         start("Notepad")
> >         write("Hello World!")
> >         press(CTRL + 's')
> >         write("test.txt", into="File name")
> >         click("Save")
> >         press(ALT + F4)
> 
> > over
> 
> >         notepad = start("Notepad")
> >         notepad.write("Hello World!")
> >         notepad.press(CTRL + 's')
> >         notepad.write("test.txt", into="File name")
> >         notepad.click("Save")
> >         notepad.press(ALT + F4)?
> 
> You are not the only one.
> 
> I suggest that you have a set of functions that work on "the current 
> window", whatever that is. Preferably there should always be a current 
> window, but if not, ensure that you give a clear error message.

This is exactly the way it is currently. I am glad you also see it that way.

> Then you have syntax for operating on any named(?) window. So a user can 
> implicitly operate on the current window:
> 
> select(notepad)
> write("goodbye cruel world")
> save()

One idea would be to use the Window(...) constructor to select windows:

    notepad = Window('Untitled - Notepad')
    select(notepad)
    save()

One advantage of using a global method to switch to a window is that this would allow you to directly switch to a Window without having to call Window(...):

    switch_to('Untitled - Notepad')

I'm still not fully convinced of a global method though, for the reasons several people here have already mentioned.

> or explicitly on any window they like:
> 
> excel.quit()

This example makes it look likely that there will have to be other operations that can be performed on windows (/running applications as Dave pointed out). So, a 'quit()' method that closes a window in addition to the already mentioned focus/select/activate method. This in turn makes the global function less attractive as once we start going down that route, global functions will proliferate.

> I suggest you dig up an old book on "Hypercard", for Apple Macs in the 
> 1980s and 90s. Back in the day, Macs could only run a single application 
> at a time, and Hypercard was limited to a single window at a time (called 
> a "stack"). But that stack (think: window) could have multiple 
> "cards" (think: window tabs), one of which was always current. 
> Hypercard's built-in programming language Hypertalk let you do things 
> like this:
> 
> go to stack "Notepad"
> type "goodbye cruel world" in field "main" of card 7
> click button "Save"
> click button "Quit" of card "Main" of stack "Excel"
> 
> (more or less... it's been a few years since I've had a classic Mac 
> capable of running Hypercard.)

Very interesting. I had never heard of HyperCard. I read up on it a little and it sounds very similar to the model we are internally building of open applications (stacks) and their windows (cards). Also funny that HyperCard was one of Ward Cunningham's inspirations for coming up with the Wiki idea: http://c2.com/cgi/wiki?WikiWikiHyperCard

Thanks for this!

Michael
www.getautoma.com



More information about the Python-list mailing list