On Dec 18, 1:27 pm, Robert Kern <robert.k...@gmail.com> wrote: > Mikael Olofsson wrote: > > Diez B. Roggisch wrote: > >> Yep. And it's easy enough if you don't care about them being different.. > > >> def __repr__(self): > >> return str(self) > > > If I ever wanted __str__ and __repr__ to return the same thing, I would > > make them equal: > > > def __str__(self): > > return 'whatever you want' > > __repr__ = __str__ > > > That makes it more obvious to me what's going on. As a bonus, it saves > > one method call for every repr call. > > Or just define __repr__(). str() falls back to __repr__() if __str__() is not > defined. > > -- > Robert Kern > > "I have come to believe that the whole world is an enigma, a harmless enigma > that is made terrible by our own mad attempt to interpret it as though it > had > an underlying truth." > -- Umberto Eco
I learned a while ago to just define __repr__, and let __str__ revert to it by default. Then, when I print a list for debugging purposes, I see something useful (as opposed to a type name and an address). The only reason that I can think of to define __str__ is to get two different representations. For example, sometimes I need both a one- line output format and a multi-line option for more clarity or detail. Then I use __repr__ for the one-liner and __str__ for the multi-liner. -- http://mail.python.org/mailman/listinfo/python-list