Help me pick an API design (OO vs functional)

Michael Herrmann michael.herrmann at getautoma.com
Tue Mar 26 10:06:18 CET 2013


On Tuesday, March 26, 2013 12:11:34 AM UTC+1, Ethan Furman wrote:
> On 03/25/2013 12:29 PM, Michael Herrmann wrote:
> ...
> >
> > 	notepad_1 = start("Notepad")
> > 	notepad_2 = start("Notepad")
> > 	notepad_1.write("Hello World!")
> > 	notepad_1.press(CTRL + 'a', CTRL + 'c')
> > 	notepad_2.press(CTRL + 'v')
> 
> This is the way to go.  Just move your global functions into the Window object (or whatever you call it), break 
> backwards compatibility (major version number change, perhaps?), and call it good.
> 
> It makes much more sense to call methods of several different objects (which is explicit -- you always know which object 
> is being used) than having a magic function that changes the object in the background (plus you now have to search 
> backwards for the last magic invocation to know -- and what if a called function changes it?).

Your points are valid and I cannot really say anything against them. The problem with moving all global functions into the Window object is that this adds a lot of syntactic baggage that isn't needed in 90% of the cases. We really prefer 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).

Also, there's a problem here: The "Save" dialogue that opens in the above script is technically a different window so in theory you would have to introduce a new object to distinguish between the original window that lets you edit your text document from the "Save" window. This is very tedious and error-prone. You are right, though, that we have to do some logic in the background to remember the last window.

In light of this, could you live with something along the lines of design #4?

        notepad_1 = start("Notepad") 
        notepad_2 = start("Notepad") 
        notepad_1.focus() 
        write("Hello World!") 
        press(CTRL + 'a', CTRL + 'c') 
        notepad_2.focus() 
        press(CTRL + 'v') 

Thanks,
Michael



More information about the Python-list mailing list