пятница, 28 декабря 2012 г., 14:37:46 UTC+6 пользователь Aymeric Augustin 
написал:
>
> 2012/12/28 Karen Tracey <kmtr...@gmail.com <javascript:>>
>
>> On Thu, Dec 27, 2012 at 7:15 PM, Aymeric Augustin <
>> aymeric....@polytechnique.org <javascript:>> 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.
>
>
There is no a single "system charset" and that's the problem :) 

>>> import sys>>> import locale>>> sys.getdefaultencoding()'ascii'>>> 
>>> locale.getpreferredencoding()'cp1251'>>> sys.stdout.encoding'cp866'

(REPL in Windows XP with Russian locale with all defaults; 
sys.stdout.encoding would be None for scripts). It is not possible (as far 
as I can tell) to encode __str__ or __repr__ results to some encoding 
(except 7bit ascii) and expect it to work reliably.

 

> -- 
> Aymeric.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/Tl53rAjzILQJ.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to