Help me pick an API design (OO vs functional)

Chris Angelico rosuav at gmail.com
Wed Mar 27 07:44:49 EDT 2013


On Wed, Mar 27, 2013 at 7:55 PM, Michael Herrmann
<michael.herrmann at getautoma.com> wrote:
> On Tuesday, March 26, 2013 5:41:42 PM UTC+1, Dave Angel wrote:
>> To go back to my sample wrapper functions, they'd look something like
>> (untested):
>>
>> def write(*args, focus=focused):
>>      focus.write(*args)
>
> I understood what you meant - I'm not so worried about the invocations, as of course the parameter can be omitted if there's a default value/behaviour. What I am worried about is the complexity this approach adds to several functions. Yes, you could argue that one keyword argument really isn't that much, but then you have to maintain and document it for all functions that have the new keyword parameter. In other words, a single functionality that is not needed 90% of the time increases the complexity of several, not really related functions. I am very grateful for your suggestions! But I don't think adding this keyword parameter is the way to go for us.
>

Not seeking to advocate this particular option, but it would be
possible to make a single wrapper for all your functions to handle the
focus= parameter:

def focusable(func):
	@functools.wraps(func)
	def wrapper(*args,focus=None):
		if focus: focus.activate()
		return func(*args)
	return wrapper

Then you just decorate all your functions with that:
def write(string):
    # do something with the active window

ChrisA



More information about the Python-list mailing list