Help me pick an API design (OO vs functional)

Ethan Furman ethan at stoneleaf.us
Wed Mar 27 12:45:49 EDT 2013


On 03/27/2013 02:34 AM, Michael Herrmann wrote:
> After everybody's input, I think Design #2 or Design #4 would be the best fit for us:
>
> Design #2:
>          notepad_1 = start("Notepad")
>          notepad_2 = start("Notepad")
>          switch_to(notepad_1)
>          write("Hello World!")
>          press(CTRL + 'a', CTRL + 'c')
>          switch_to(notepad_2)
>          press(CTRL + 'v')
>
> Design #4:
>          notepad_1 = start("Notepad")
>          notepad_2 = start("Notepad")
>          notepad_1.activate()
>          write("Hello World!")
>          press(CTRL + 'a', CTRL + 'c')
>          notepad_2.activate()
>          press(CTRL + 'v')
>
> Normally, I'd go for Design #4, as it results in one less global, is better for autocompletion etc. The thing with our library is that it tries to make its scripts as similar as possible to giving instructions to someone looking over their shoulder at a screen. And in this situation you would just say
>
>         activate(notepad)
>
> rather than
>
>         notepad.activate().
>
> So the problem lies in a difference between Python's and English grammar. For beauty, I should go with #2. For pragmatism, I should go with #4. It hurts, but I'm leaning towards #4. I have to think about it a little.

Go with #2.  Not everything has to be a method.  len(), for example, is not a method, even though it calls one.

The 'with' support would also be cool.  __enter__ sets the new global window object whilst saving the old one, and then 
__exit__ restores the old one.

--
~Ethan~



More information about the Python-list mailing list