os.path.join

half.italian at gmail.com half.italian at gmail.com
Wed May 2 03:50:45 EDT 2007


On May 2, 12:36 am, Ant <ant... at gmail.com> wrote:
> On May 2, 8:03 am, half.ital... at gmail.com wrote:
>
> > On May 1, 11:10 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> ...
> > > I think it's a bug, but because it should raise TypeError instead.
> > > The right usage is os.path.join(*pathparts)
> ...
> > Wow.  What exactly is that * operator doing?  Is it only used in
> > passing args to functions?  Does it just expand the list into
> > individual string arguments for exactly this situation?  Or does it
> > have other uses?
>
> It's used for unpacking a collection into arguments to a function.
> It's also used at the other end for receiving a variable length set of
> arguments. i.e.
>
> >>> x = (1,3)
> >>> def add(a, b):
>
>         return a + b
>
> >>> add(*x)
> 4
> >>> def add(*args):
>
>         return reduce(int.__add__, args)
>
> >>> add(1,2,3,4,5,6)
> 21
> >>> add(*x)
>
> 4
>
> The same sort of thing holds for keyword arguments:
>
> >>> def print_kw(**kw):
>
>          for k in kw:
>              print kw[k]
>
> >>> print_kw(a=1, b=2)
>
> 1
> 2>>> d = {'a': 1, 'b': 10, 'c': 100}
> >>> print_kw(**d)
>
> 1
> 100
> 10

Thank you both.




More information about the Python-list mailing list