Help me pick an API design (OO vs functional)

Chris Angelico rosuav at gmail.com
Tue Mar 26 07:38:35 EDT 2013


On Tue, Mar 26, 2013 at 7:53 PM, Michael Herrmann
<michael.herrmann at getautoma.com> wrote:
> On Monday, March 25, 2013 10:08:53 PM UTC+1, Chris Angelico wrote:
>> ...
>> > I kind of like the context manager solution because the indentation makes it very obvious what happens in which window. You are right about our target group though. Also, the "with" is not as explicit as it probably should be.
>>
>> What happens at the __exit__ of the context manager? What happens if
>> context managers are nested? I'd be inclined to the simpler option of
>> an explicit switch (since focus doesn't really "stack" and it'd feel
>> weird for focus to *sometimes* switch away when you're done working
>> with one window), though the context manager syntax does have its
>> advantages too.
>
> You are right, an __exit__ for a window doesn't really make sense and neither does stacking. There's also the problem that the focus window may change - for instance when closing it. What happens if you're still inside the "with ..." then? At first glance, I think the context manager solution looks nice syntactically, but maybe it isn't the way to go here.

Fundamental point: As I understand the API, it doesn't *actually* tie
to a window. You don't locate the Notepad window and send it keys -
you switch focus to Notepad and then send keys to the whole system. Is
this correct? I'm basing my understanding on this paragraph from your
original post:
> We do not (yet) have a functionality that allows you to explicitly switch to a
> specific window. Such a functionality would for instance make it possible to
> open two Notepad windows using the start(...) command, and copy text
> between them.

If so, then all of the method-based options are effectively lying,
because they imply a binding that's not there. The actual sequence of
actions includes imperatives of "switch to some other window", so I
think that's what the API should reflect. Otherwise, there's risk that
something will get horribly horribly confused between the programmer's
brain and the end result (which could happen on either side of your
code).

But if you can unambiguously identify a running instance of something
and switch to it, then a method on the object that start() returns
would be absolutely correct. So I'd be looking at either your second
or fourth options from the original post.

ChrisA



More information about the Python-list mailing list