Near Final PEP 247 - API for Cryptographic Hash Functions

Donn Cave donn at drizzle.com
Sat Sep 22 01:20:39 EDT 2001


Quoth Robert Amesz <reqhye72zux at mailexpire.com>:
| Andrew Kuchling wrote:
|
|> Second, why should __str__ be the output of .hexdigest() and not
|> .digest(), which also returns a string?
|
| See the docs:
|
|       str(object) 
|    Return a string containing a nicely printable representation of an
|    object. For strings, this returns the string itself. The
|    difference with repr(object) is that str(object) does not always
|    attempt to return a string that is acceptable to eval(); its goal
|    is to return a printable string.
|
| Since the raw digest can (and probably will) contain both control 
| characters (< 32) and high-ASCII (> 126) it can't be said te be 'nicely 
| printable'.

That doc is one thing, and common practice is another.  I would go
so far as to say, the doc is wrong.

There are a bunch of different objectives for "self to string" methods,
and only two standard methods.  In the standard library, when I last
looked I found a lot of cases where __str__ is the object's data as
a string, for use in a computation that requires a string.  For me,
that is, by de facto convention, what __str__ means.  The above
really describes how __repr__ is ordinarly used, and the notion
that it's for eval() isn't really born out by usage.

	Donn Cave, donn at drizzle.com



More information about the Python-list mailing list