Automatically remove named parameters from a Python function call -
i've written python library uses lot of named parameters in function calls, , i'm wondering if there's way automatically remove of these named parameters, instead of removing them manually (which tedious task).
for example, possible translate function call:
getclass(lang="java", body=[ mainmethod(lang="java", body=[ println(lang="java", toprint="hello world") ]) ])
into function call below (which omits named arguments, , more concise?):
getclass("java", [ mainmethod("java", [ println("java", "hello world!") ]) ])
in order this, 1 possible approach write function print own function call string - however, don't think it's possible write function this. there other approaches problem might work well?
there no need. keyword arguments can still passed positionally. there added benefit, here, of being able specify 1 or none of them, if please. however, don't need specify @ all.
>>> def foo(bar=1, baz=[2,3]): ... print bar, baz ... >>> foo() 1 [2, 3] >>> foo(baz=4) 1 4 >>> foo(10, 20) 10 20
if i'm misunderstanding code you're providing, let me know. steve's answer seems indicate working strings, didn't see in post indicate that.
you did mention function prints own function call; this, assume mean function must print string looks thing you'd type call function same arguments passed. relatively easy do, because can either type function's name as-is, or can use __name__
attribute.
>>> def goo(a,b): ... print "{}({}, {})".format(goo.__name__, a, b) ... >>> goo(1,2) goo(1, 2) >>> def hoo(*args): ... print "{}({})".format(hoo.__name__, ', '.join((str(arg) arg in args))) ... >>> >>> hoo(2,3,4,5) hoo(2, 3, 4, 5)
thinking it, example seems wanting of general function grant behavior function - recursively. here's way achieve that, using partial
s (i'll redefine foo()
example makes more sense):
>>> functools import partial >>> def foo(a, b): ... return (a if not isinstance(a, partial) else a()) + (b if not isinstance(b, partial) else b()) ... >>> fun = partial(foo, 1, partial(foo, partial(foo, 2, 4), partial(foo, 3, 5))) >>> fun() 15 >>> fun = partial(foo, 1, partial(foo, partial(foo, 2, 4), partial(foo, 3, 5))) >>> def print_pfunc(pfunc): ... return "{}({}{}{})".format(pfunc.func.__name__, ', '.join(str(arg) if not isinstance(arg, partial) else print_pfunc(arg) arg in pfunc.args) if pfunc.args else '', ', ' if pfunc.args , pfunc.keywords else '', ', '.join('{}={}'.format(k, v if not isinstance(v, partial) else print_pfunc(v)) k, v in pfunc.keywords) if pfunc.keywords else '') ... >>> print print_pfunc(fun) foo(1, foo(foo(2, 4), foo(3, 5)))
if you're not fan of long format()
call, here's different way write (just don't have spend time decoding garbage):
def print_pfunc(pfunc): args = "" if pfunc.args not none: args = ', '.join(str(arg) if not isinstance(arg, partial) else print_pfunc(arg) arg in pfunc.args) kwargs = "" if pfunc.keywords not none: kwargs = ', '.join('{}={}'.format(k, v if not isinstance(v, partial) else print_pfunc(v)) k, v in pfunc.keywords) return "{}({}{}{})".format(pfunc.func.__name__, args, ', ' if args , kwargs else '', kwargs)
adapting code, now, require write code turn function calls partials before evaluating them. it's want point - can't think of clever way around fact function calls passed arguments evaluated before passed, interfere trying do.
Comments
Post a Comment