[Python-3000] Types and classes
Terry Reedy
tjreedy at udel.edu
Tue Apr 8 05:04:12 CEST 2008
"Greg Ewing" <greg.ewing at canterbury.ac.nz> wrote in message
news:47FAD002.8080306 at canterbury.ac.nz...
| Terry Reedy wrote:
|
| > As in
| >
| >>>>print(type(3))
| >
| > int # instead of <class 'int'>
|
| I have the same feeling there -- the only time I'm
| likely to be deliberately printing a class is for
| debugging, and then I want unambiguity.
Unfortunately, *any* text printed for any object *could* have been the
value of a string object. With str(), ambiguity is rife:
>>> a = '1'
>>> b = 1
>>> print(a,b)
1 1
So if you want unabmiguity for debugging, repr() is better since strings
and only strings are surrounded by quotes.
>>> print(repr(a),repr(b))
'1' 1
Guido only suggested the possibility of a more-friendly abbreviation for
str, not for repr. When one calls type(x), one *knows* the answer is a
class, so the boilerplate template is often redundant and unnecessary.
Consider the current
>>> print('Expected', type(a), '; got', type(b))
Expected <type 'str'> ; got <type 'int'>
-or future-
Expected <class 'str'> ; got <class 'int'>
I would like to have the option of getting more normal looking text like
Expected str ; got int
without having to parse away the added boilerplate. If I wanted 'class' in
the output, I might prefer to put it in the strings to get
Expected class str ; got class int
without the brackets.
Should print() have an option to convert with repr instead of str?
Terry Jan Reedy
More information about the Python-3000
mailing list