Hello,
Thanks for all your answers. A decorator will indeed be the cleanest solution.
This idea was suggested on IRC too but withdrawn because "you can't bind back
to the class". Well, as far as I can tell, the code below works. Please let me
know if you see any issues with this implementation.
# django.utils.encoding
from django.utils import six
def python_2_unicode_compatible(klass):
if not six.PY3:
klass.__unicode__ = klass.__str__
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
return klass
# in Django or user code
from __future__ import unicode_literals
from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class MyClass(object):
def __str__(self):
result = "text ..."
The only drawback of this solution is that it hardcodes the 'utf-8' encoding. I
don't see that as a problem since Django already makes this assumption
everywhere, especially in the smart_text/bytes functions.
The digression on the tutorial is interesting. I'd like to reach the "Py3 with
Py2 warnings" step as fast as possible but we can't do that until a significant
part of the Django ecosystem has itself migrated to Python 3.
Anyway, I'm not working on the documentation at this time. I'm only keeping API
docs in sync with the code. This will be easier to discuss when the port is
finished, and we have a better view of its consequences.
Best regards,
--
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.