A short question about non-ascii characters in list

Discussion in 'Python' started by js, Sep 17, 2007.

  1. js

    js Guest

    >>> print u"äöü"
    äöü
    >>> print [u"äöü"]

    [u'\xe4\xf6\xfc']

    Python seems to treat non-ASCII chars in a list differently from the
    one in the outside of a list.
    I think this behavior is so inconvenient and actually makes debugging
    work harder.

    Is this an intentional? Is there any doc discussing about this?

    Thanks.
     
    js, Sep 17, 2007
    #1
    1. Advertising

  2. js

    Dan Bishop Guest

    On Sep 17, 12:08 am, js <> wrote:
    > >>> print u"äöü"

    > äöü
    > >>> print [u"äöü"]

    >
    > [u'\xe4\xf6\xfc']
    >
    > Python seems to treat non-ASCII chars in a list differently from the
    > one in the outside of a list.
    > I think this behavior is so inconvenient and actually makes debugging
    > work harder.
    >
    > Is this an intentional? Is there any doc discussing about this?


    It's intentional. __str__ of a list uses the __repr__ of its
    elements. This helps reduce confusion (e.g., between ['a', 'b, c']
    and ['a, b', 'c']).
     
    Dan Bishop, Sep 17, 2007
    #2
    1. Advertising

  3. js

    js Guest

    Thank you for your quick reply.

    > It's intentional. __str__ of a list uses the __repr__ of its
    > elements. This helps reduce confusion (e.g., between ['a', 'b, c']
    > and ['a, b', 'c']).

    That's make sence, but it's also true that
    sometimes we want to see the contents of a list in pretty format.
    So for now I need to write and use crappy mylist like this.

    class mylist(list):
    def __str__(self):
    return '[' + ', '.join(self) + ']'

    l = mylist([u"äöü", u"äöü", u"äöü"])
    print unicode(l)


    very ugly, but just works.
     
    js, Sep 17, 2007
    #3
  4. js wrote:
    > That's make sence, but it's also true that
    > sometimes we want to see the contents of a list in pretty format.


    That may be true, but most of the time not (at least not me) --
    lists are no pretty printing instrument, but a container.

    > So for now I need to write and use crappy mylist like this.
    >
    > class mylist(list):
    > def __str__(self):
    > return '[' + ', '.join(self) + ']'
    >
    > l = mylist([u"äöü", u"äöü", u"äöü"])
    > print unicode(l)
    >
    >
    > very ugly, but just works.


    What's wrong with

    print ",".join(mylist)

    ? Also, the your solution isn't really "ugly" in my opinion.

    Regards,


    Björn

    --
    BOFH excuse #55:

    Plumber mistook routing panel for decorative wall fixture
     
    Bjoern Schliessmann, Sep 17, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TOXiC
    Replies:
    5
    Views:
    1,284
    TOXiC
    Jan 31, 2007
  2. Laszlo Nagy
    Replies:
    6
    Views:
    639
  3. Alextophi
    Replies:
    8
    Views:
    545
    Alan J. Flavell
    Dec 30, 2005
  4. bruce
    Replies:
    38
    Views:
    293
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    102
Loading...

Share This Page