Factory function with keyword arguments

Paul Rubin http
Sat Sep 22 23:14:20 EDT 2007


Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:
> 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.



More information about the Python-list mailing list