Factory function with keyword arguments

S

Steven D'Aprano

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.
 
P

Paul Rubin

Steven D'Aprano said:
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.
 
G

George Sakkis

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
 
R

Ron Adam

Steven said:
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
 
R

Ron Adam

Steven said:
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
 
S

Steven D'Aprano

[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.
 
B

Ben Finney

Steven D'Aprano said:
A valuable lesson... always measure before guessing whether code
will be slow or not.

And after measuring, don't guess then either :)
 
R

Ron Adam

Steven said:
[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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
ScottChare

Latest Threads

Top