Help me pick an API design (OO vs functional)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Mar 26 23:37:23 CET 2013


On Tue, 26 Mar 2013 07:33:18 -0700, Michael Herrmann wrote:

>> As long as you know the program is going to be simple, pile on the
>> globals.  But as soon as it advances, each of them is a trap to fall
>> into.
> 
> You're right with everything you say. globals are bad and it may happen
> that this will bite me.

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 recommend you look at the random.py 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.


-- 
Steven



More information about the Python-list mailing list