Python function as a variable - different number of arguments -
maybe weird question. have code this:
def foo():   print "foo"  def bar(x):   print x  func = foo func() func = bar arg = 'a' func(arg) is there way have "empty" argument, can call assign foo() func , still call func(arg)?
not workaround
if arg none:    func() else:    func(arg) 
sure, if use tuples/lists , argument unpacking.
def foo():   print "foo"  def bar(x):   print x  args = () func = foo func(*args)  func = bar args = ('a',) func(*args) result:
foo 
Comments
Post a Comment