Extending Python - variable sized arrays?
Skip Montanaro
skip at pobox.com
Tue Oct 16 18:47:50 EDT 2001
Paul> Use the apply function. apply(f, [1,2,3]) is the same as f(1,2,3).
In recent versions (>= 2.0 I think), you can dispense with apply() in many
situations:
>>> def f(*args):
... print args
...
>>> apply(f, (1,2,3))
(1, 2, 3)
>>> f(*(1,2,3))
(1, 2, 3)
Same technique works for keyword args:
>>> def g(**kwds):
... for k in kwds:
... print (k, kwds[k])
...
>>> apply(g, (), {"a":1, "b":2})
('a', 1)
('b', 2)
>>> g(**{"a":1, "b":2})
('a', 1)
('b', 2)
Generally speaking, neither technique is used with literals as I've shown,
but to pass variable arg lists along to other varargs functions:
class Foo(Bar):
def __init__(self,a,b,c,*args,**kwds):
Bar.__init__(self,a,b,c,*args,**kwds)
...
--
Skip Montanaro (skip at pobox.com)
http://www.mojam.com/
http://www.musi-cal.com/
More information about the Python-list
mailing list