Help me pick an API design (OO vs functional)

Michael Herrmann michael.herrmann at
Wed Mar 27 10:34:09 CET 2013

On Tuesday, March 26, 2013 11:37:23 PM UTC+1, Steven D'Aprano wrote:
> Global *variables* are bad, not global functions. You have one global 
> variable, "the current window". So long as your API makes it obvious when 
> the current window changes, implicitly operating on the current window is 
> no more dangerous than Python's implicit operations on the current 
> namespace (e.g. "x = 2" binds 2 to x in the current namespace).

I'm generally wary of everything global, but you're right as long as no (global) state is involved. 

> I recommend you look at the API. You have a Random class, that 
> allows the user to generate as many independent random number generators 
> as needed. And the module also initialises a private instance, and 
> exposes the methods of that instance as top-level functions, to cover the 
> 90% simple case where your application only cares about a single RNG.

I looked it up - I think this is a very good approach; to provide easy access to the functionality used in 90% of cases but still give users the flexibility to cover the edge cases.

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") 
        write("Hello World!") 
        press(CTRL + 'a', CTRL + 'c') 
        press(CTRL + 'v') 

Design #4: 
        notepad_1 = start("Notepad") 
        notepad_2 = start("Notepad") 
        write("Hello World!") 
        press(CTRL + 'a', CTRL + 'c') 
        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


rather than


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.

Thank you so much to everybody for your inputs so far!

More information about the Python-list mailing list