[Tutor] %s %r with cutom type
spir
denis.spir at gmail.com
Fri Mar 12 12:29:17 CET 2010
Hello again,
A different issue. On the custom Unicode type discussed in another thread, I have overloaded __str__ and __repr__ to get encoded byte strings (here with debug prints & special formats to distinguish from builtin forms):
class Unicode(unicode):
ENCODING = "utf8"
def __new__(self, string='', encoding=None):
if isinstance(string,str):
encoding = Unicode.ENCODING if encoding is None else encoding
string = string.decode(encoding)
return unicode.__new__(Unicode, string)
def __repr__(self):
print '+',
return '"%s"' %(self.__str__())
def __str__(self):
print '*',
return '`'+ self.encode(Unicode.ENCODING) + '`'
An issue happens in particuliar cases, when using both %s and %r:
s = "éâÄ"
us = Unicode(s)
# str
print us,
print str(us),
print us.__str__(),
print "%s" %us
# repr
print repr(us),
print us.__repr__(),
print "%r" %us
# both
print "%s %r" %(us,us)
==>
éâÄ * `éâÄ` * `éâÄ` éâÄ
+ * "`éâÄ`" + * "`éâÄ`" + * "`éâÄ`"
+ *
Traceback (most recent call last):
File "Unicode.py", line 38, in <module>
print "%s%r" %(us,us)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)
Note that Unicode.__str__ is called neither by "print us", nore by %s. What happens? Why does the issue only occur when using both format %s & %s?
If I replace the last line by "print "%s %r" %(str(us),us)", all works fine. But then what's the point with %s? And why doesn't print alone call __str__?
Denis
More information about the Tutor
mailing list