On Sat, 2006-04-29 at 14:49 +0200, Petar Marić wrote:
> Looking through some recent changes I noticed that some M-R code uses
> __repr__ and some new checkins use __str__
> 
> Is there anything we need to know?

Short answer:

You need to know that Django is coming around to the Pythonic way of
doing things. :-)

Use __str__ where you want a friendly string to appear. Don't use
__repr__ where you should be using __str__.

Longer answer: (you probably know all this, but let's get it in the
archives anyway)...

In general (in Python), __str__ is a method that returns a
user-friendly, human-readable string representing the class. __repr__
returns something that is lower-level, more machine processable (the
Python docs recommend that __repr__ either returns a string that can be
used as a Python expression to recreate the class, or a string of the
form "<...some useful description...>").

In Django terms, this means that the useful string you see in Admin
classes and elsewhere should really be the thing returned by __str__ and
that __repr__ should be left alone to return something like "<Entry
object>". A while back now, changes were checked into the admin
component and elsewhere to ensure that __str__ was used wherever
necessary (note that __str__ falls back to using __repr__ if the former
is not defined, so the change was backwards compatible). If anything
remains that should be presenting a user readable string and it is using
a %r formatter or calling repr() explicitly, that is a bug. However, it
was not deemed high priority to go back and retrofit all the existing
code to implement __str__ instead of __repr__, since there is so much
else to do.

You will notice, however, that some of Adrian's recent checkins as he is
proofreading the documentation change the suggestion so that people are
encouraged to use __str__ where appropriate.

Cheers,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

Reply via email to