zip() or what?

Sean Ross sross at connectmail.carleton.ca
Thu Jul 3 10:47:04 EDT 2003


"Ray Tomes" <rtomes at ihug.co.nz> wrote in message
news:3F0401C4.1010306 at ihug.co.nz...
>... I don't get the meaning of the * [snip]. Is this like the opposite of
putting [] around something or what?

That's pretty much it. I think of it as an unpacking operator (which it
isn't, but anyway...). You can use it to unpack a list or tuple of arguments
that you want to feed into a function [1]. Conversely, you can use it when
defining a function to say "this function also takes an arbitrary number of
additional arguments"[2].

You've seen an example of [1] for your own problem, here's another quick
one:

def foo(a, b, c):
    print "%s %s %s" % (a, b, c)
argfeed = ['a', 'b', 'c']
foo(*argfeed)    # equivalent to foo('a', 'b', 'c')

And an example of [2] would be:

def foo(*args):
    for arg in args:
        print arg
foo('a', 'b', 'c', 'hello world')

The second useage is in the tutorial [ section 4.7.3 ], I'm not sure where
the first useage is.
HTH
Sean






More information about the Python-list mailing list