2012/12/28 Karen Tracey <[email protected]>

> On Thu, Dec 27, 2012 at 7:15 PM, Aymeric Augustin <
> [email protected]> wrote:
>
>> 2) under Python 2.x __str__ is implemented as __unicode__
>>    encoded to utf8.
>>
>>
>> Yes, this is a legacy behavior that I strongly disagree with. It
>> makes __str__ / __unicode__ handling for Model subclasses
>> exceedingly sketchy.* The only reason why I didn't remove
>> it is backwards compatibility.
>>
>
> I'm curious what would you do instead, if backward-incompatibility were
> not a concern?


In Python __unicode__ is __str__ + decode(sys.getdefaultencoding()):
http://docs.python.org/2/reference/datamodel.html#object.__unicode__

Django reverses this behavior for Model subclasses and uses utf-8.

I was first exposed to unicode handling in Python via Django and
it took me a long time to discover that writing a __unicode__ method
wasn't the canonical way to define an object's string representation.

If we set aside backwards-compatibility, I would probably
define a conventional, Django-specific method (eg. __display__)
and derive __unicode__ and __str__ from there, according to
Django's rules. Not using Python's default names makes it explicit
that Django adds specific behavior. This vastly simplifies the
definition of __str__ and __unicode__ in Python 2 and 3, and also
avoids the semantic issue of __str__ returning unicode on Python 2
under @python_2_unicode_compatible.

An alternative would be to provide an explicit way to change
Python's behavior -- either a decorator or a class attribute.
Though this isn't going to be very DRY.

I haven't had issues with using utf-8 instead of the system charset,
but Mikhail seems to suggest it can be a problem on Windows.
Using the system charset, like Python does, might be less surprising.
I don't have enough experience to tell. This won't be a problem for
displaying objects in templates, because they're rendered in unicode.

-- 
Aymeric.

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to