A short question about non-ascii characters in list

J

js

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.
 
D

Dan Bishop

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']).
 
J

js

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.
 
B

Bjoern Schliessmann

js said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,619
Members
45,310
Latest member
FaustoMont

Latest Threads

Top