.join() question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • drs

    .join() question

    why does
    [color=blue][color=green][color=darkred]
    >>> ''.join(lst)[/color][/color][/color]

    not automatically do
    [color=blue][color=green][color=darkred]
    >>> ''.join([str(i) for i in lst])[/color][/color][/color]

    ? That is, is there ever a time when one does not want .join to make
    everything into a string? This seems like reasonable default behavior, but
    maybe I'm missing something?

    -doug


  • Terry Reedy

    #2
    Re: .join() question


    "drs" <drs@ecp.cc> wrote in message
    news:eo_Oa.1441 5$BM.4645038@ne wssrv26.news.pr odigy.com...[color=blue]
    > why does
    >[color=green][color=darkred]
    > >>> ''.join(lst)[/color][/color]
    >
    > not automatically do
    >[color=green][color=darkred]
    > >>> ''.join([str(i) for i in lst])[/color][/color]
    >
    > ? That is, is there ever a time when one does not want .join to[/color]
    make[color=blue]
    > everything into a string? This seems like reasonable default[/color]
    behavior, but[color=blue]
    > maybe I'm missing something?[/color]

    If the members of lst are already str strings, this would be a waste
    of time. If they are unicode strings, this would be the wrong thing
    to do. Auto conversions are not Python's style.

    Terry J. Reedy


    Comment

    • Simon Bayling

      #3
      Re: .join() question

      I would risk a guess that this comes under the "explicit is better than
      implicit" guideline.

      ''.join([str(i) for i in lst])

      Explains to everyone that you know the list has some non-string things,
      and you intend them to be str()'ified.

      Intention-guessingly yours,
      Simon.

      "drs" <drs@ecp.cc> wrote in
      news:eo_Oa.1441 5$BM.4645038@ne wssrv26.news.pr odigy.com:
      [color=blue]
      > why does
      >[color=green][color=darkred]
      >>>> ''.join(lst)[/color][/color]
      >
      > not automatically do
      >[color=green][color=darkred]
      >>>> ''.join([str(i) for i in lst])[/color][/color]
      >
      > ? That is, is there ever a time when one does not want .join to make
      > everything into a string? This seems like reasonable default
      > behavior, but maybe I'm missing something?
      >
      > -doug
      >
      >
      >[/color]

      Comment

      • drs

        #4
        Re: .join() question

        "Terry Reedy" <tjreedy@udel.e du> wrote in message
        news:Q4KcnQcI8s gt65GiXTWJiA@co mcast.com...[color=blue]
        >
        > "drs" <drs@ecp.cc> wrote in message
        > news:eo_Oa.1441 5$BM.4645038@ne wssrv26.news.pr odigy.com...[color=green]
        > > why does
        > >[color=darkred]
        > > >>> ''.join(lst)[/color]
        > >
        > > not automatically do
        > >[color=darkred]
        > > >>> ''.join([str(i) for i in lst])[/color]
        > >
        > > ? That is, is there ever a time when one does not want .join to[/color]
        > make[color=green]
        > > everything into a string? This seems like reasonable default[/color]
        > behavior, but[color=green]
        > > maybe I'm missing something?[/color]
        >
        > If the members of lst are already str strings, this would be a waste
        > of time. If they are unicode strings, this would be the wrong thing
        > to do. Auto conversions are not Python's style.[/color]
        [color=blue][color=green][color=darkred]
        >>> l = [u'abcde', 'fghij']
        >>> ''.join(l)[/color][/color][/color]
        u'abcdefghij'

        this seems to be auto converting the non-unicode to unicode?

        -d


        Comment

        Working...