[issue5050] unicode(C) invokes C.__unicode__ when __unicode__ is defined

Yu-Jie Lin report at bugs.python.org
Sun Jan 25 06:40:41 CET 2009


New submission from Yu-Jie Lin <livibetter at gmail.com>:

Run the following code

class A:
  def __str__(self):
    return "__str__"
  def __unicode__(self):
    return "__unicode__"

a = A()

print str(a), unicode(a)
print str(A), unicode(A)

----

Results on Python 2.5.4 (r254:67916):
__str__ __unicode__
__main__.A
Traceback (most recent call last):
  File "/home/livibetter/tmp/unicode_classobj.py", line 14, in <module>
    print str(A), unicode(A)
TypeError: unbound method __unicode__() must be called with A instance
as first argument (got nothing instead)

Results on Python 2.6.1 (r261:67515):
__str__ __unicode__
__main__.A __main__.A

----

I was expecting the same result as on 2.6.1. By my observation,
unicode(C) tries to invoke C.__unicode__ if __unicode__ is defined in C,
where C is a class object.

I believe this behavior is incorrect.

----------
messages: 80496
nosy: livibetter
severity: normal
status: open
title: unicode(C) invokes C.__unicode__ when __unicode__ is defined
versions: Python 2.5

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5050>
_______________________________________


More information about the Python-bugs-list mailing list