"\n" in unicode vs "regular" string: is this normal?

Eric Brunel eric.brunel at pragmadev.com
Wed Jan 29 06:25:58 EST 2003


Hi all,

Consider the following class:

class C:
   def __init__(self, s):
     self.s = s
   def __repr__(self):
     return self.s

Now run:

 >>> o1 = C("foo\nbar")
 >>> o1
foo
bar
 >>> o2 = C(unicode("foo\nbar"))
 >>> o2
foo\nbar

Is there a good reason why the result is different? The unicode stuff should 
take by default the standard encoding which is for me just plain ASCII. So why 
does the two objects have different representations?

I just stepped on this problem because of Tkinter returning plain strings if the 
text is only ASCII, but unicode strings otherwise. The text returned from 
Tkinter was wrapped in XML code by specific objects using __repr__ (which may 
not be a good idea indeed...). And it took me ages to figure out why on earth 
the text I stored in my file contained the string "\n" instead of actual line 
feeds...
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com





More information about the Python-list mailing list