However,
__unicode__
also allows returning of buffer objects, which directly object and don't inherit from basestring.
This is not correct. unicode()
can convert a string or a buffer. It is a "best attempt" at converting the passed argument to unicode using the default encoding (that's why it says coercing). It will always return a unicode object.
So, my question is, what actually happens when str and unicode are called? What are the return value requirements on
__str__
and__unicode__
for use in str and unicode?
__str__
should return an informal, human-friendly string representation of the object. This is what is called when someone uses str()
on your object, or when your object is part of a print statement.
__unicode__
should always return a unicode
object. If this method is not defined, __str__
is called and then the results are coerced to unicode (by passing them to unicode()
).
In your second example, you are returning invalid objects which is why you are seeing the error messages. Your first example appears to work for __unicode__
because of a side-effect, but it is also not written correctly.
The data model section of the documentation is worth a read for more information and details on these "magic methods".