471,331 Members | 1,477 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,331 software developers and data experts.

Factory function with keyword arguments

I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:

def factory(flag):
def foo(obj, arg):
if flag:
# use the spam keyword to method()
return obj.method(spam=arg)
else:
# use the ham keyword
return obj.method(ham=arg)
return foo

Problem: the test of which keyword to use is done every time the produced
function is called, instead of once, in the factory.

I thought of doing this:

def factory(flag):
if flag: kw = 'spam'
else: kw = 'ham'
def foo(obj, arg):
kwargs = dict([(kw, arg)])
return obj.method(**kwargs)
return foo

Is this the best way of doing this? Are there any alternative methods
that aren't risky, slow or obfuscated?

Before anyone suggests changing the flag argument to the factory to the
name of the keyword, this is only a toy example, and doing so in my
actual code isn't practical.

--
Steven.
Sep 23 '07 #1
8 1614
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrites:
def factory(flag):
if flag: kw = 'spam'
else: kw = 'ham'
def foo(obj, arg):
kwargs = dict([(kw, arg)])
return obj.method(**kwargs)
return foo
Untested:

def factory(flag):
def foo(obj, arg):
p = 'spam' if flag else 'ham'
return obj.method(**{p:arg})
return foo

is the obvious way in that style.
Sep 23 '07 #2
On Sep 22, 10:53 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:

def factory(flag):
def foo(obj, arg):
if flag:
# use the spam keyword to method()
return obj.method(spam=arg)
else:
# use the ham keyword
return obj.method(ham=arg)
return foo

Problem: the test of which keyword to use is done every time the produced
function is called, instead of once, in the factory.

I thought of doing this:

def factory(flag):
if flag: kw = 'spam'
else: kw = 'ham'
def foo(obj, arg):
kwargs = dict([(kw, arg)])
return obj.method(**kwargs)
return foo

Is this the best way of doing this? Are there any alternative methods
that aren't risky, slow or obfuscated?
Unless I'm missing something, the obvious way is to move the flag
check outside the function and have two definitions of foo:

def factory(flag):
if flag: # use the spam keyword to method()
def foo(obj, arg):
return obj.method(spam=arg)
else: # use the ham keyword
def foo(obj, arg):
return obj.method(ham=arg)
return foo

Now if foo is more than a line or two and the only difference the flag
makes is the keyword argument, you can either factor the common part
out in another function called by foo or (if performance is crucial)
create foo dynamically through exec:

def factory(flag):
kw = flag and 'spam' or 'ham'
exec '''def foo(obj, arg):
return obj.method(%s=arg)''' % kw
return foo
George

Sep 23 '07 #3


Steven D'Aprano wrote:
I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:
I thought of doing this:

def factory(flag):
if flag: kw = 'spam'
else: kw = 'ham'
def foo(obj, arg):
kwargs = dict([(kw, arg)])
return obj.method(**kwargs)
return foo

Is this the best way of doing this? Are there any alternative methods
that aren't risky, slow or obfuscated?
Looks ok to me. It can be simplified a bit.

def factory(flag):
kw = 'spam' if flag else 'ham'
def foo(obj, arg):
return obj.method(**{kw:arg})
return foo
Cheers,
Ron
Sep 23 '07 #4


Steven D'Aprano wrote:
I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:
I thought of doing this:

def factory(flag):
if flag: kw = 'spam'
else: kw = 'ham'
def foo(obj, arg):
kwargs = dict([(kw, arg)])
return obj.method(**kwargs)
return foo

Is this the best way of doing this? Are there any alternative methods
that aren't risky, slow or obfuscated?
Looks ok to me. It can be simplified a bit.

def factory(flag):
kw = 'spam' if flag else 'ham'
def foo(obj, arg):
return obj.method(**{kw:arg})
return foo
Cheers,
Ron

Sep 23 '07 #5
On 9/23/07, Ron Adam <rr*@ronadam.comwrote:
>

Steven D'Aprano wrote:
I'm writing a factory function that needs to use keywords in the produced
function, not the factory. Here's a toy example:
http://docs.python.org/whatsnew/pep-309.html
Sep 23 '07 #6
On Sun, 23 Sep 2007 03:55:45 -0500, Ron Adam wrote:
Steven D'Aprano wrote:
>I'm writing a factory function that needs to use keywords in the
produced function, not the factory. Here's a toy example:
[snip]

Thanks everyone who answered, you've given me a lot of good ideas.

I've run some tests with timeit, and most of the variants given were very
close in speed. The one exception was (not surprisingly) my version that
builds a tuple, puts it in a list, then converts it to a dict, *before*
doing anything useful with it. It was 3-4 times slower than the others.

George's version, with two definitions of foo(), was the fastest. The
second fastest was the variant using exec, which surprised me a lot. I
expected exec to be the slowest of the lot. Unfortunately, I doubt that
these would scale well as the factory becomes more complicated.

Excluding those two, the next fastest was the original code snippet, the
one I rejected as clearly too slow! It's apparently faster to check a
flag than it is build and then expand a dict for keyword arguments.

A valuable lesson... always measure before guessing whether code will be
slow or not.

--
Steven.
Sep 23 '07 #7
Steven D'Aprano <st***@REMOVE-THIS-cybersource.com.auwrites:
A valuable lesson... always measure before guessing whether code
will be slow or not.
And after measuring, don't guess then either :-)

--
\ "Science doesn’t work by vote and it doesn’t work by |
`\ authority." —Richard Dawkins, Big Mistake (The Guardian) |
_o__) |
Ben Finney
Sep 23 '07 #8


Steven D'Aprano wrote:
On Sun, 23 Sep 2007 03:55:45 -0500, Ron Adam wrote:
>Steven D'Aprano wrote:
>>I'm writing a factory function that needs to use keywords in the
produced function, not the factory. Here's a toy example:

[snip]

Thanks everyone who answered, you've given me a lot of good ideas.

I've run some tests with timeit, and most of the variants given were very
close in speed. The one exception was (not surprisingly) my version that
builds a tuple, puts it in a list, then converts it to a dict, *before*
doing anything useful with it. It was 3-4 times slower than the others.

George's version, with two definitions of foo(), was the fastest. The
second fastest was the variant using exec, which surprised me a lot. I
expected exec to be the slowest of the lot. Unfortunately, I doubt that
these would scale well as the factory becomes more complicated.
The one with exec (the others less so) will depend on the ratio of how
often the factory is called vs how often the foo is called. If the factory
is called only once, then exec only runs once. If the factory is called
every time foo is needed, then it will be much slower. So your test needs
to take into account how the factory function will be used in your program
also.

Ron

Sep 23 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

39 posts views Thread by Marco Aschwanden | last post: by
6 posts views Thread by Boogie El Aceitoso | last post: by
3 posts views Thread by domeceo | last post: by
10 posts views Thread by Chris Croughton | last post: by
5 posts views Thread by ma740988 | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.