[Python-bugs-list] [ python-Bugs-674404 ] repr of type and class objects

SourceForge.net noreply@sourceforge.net
Sat, 25 Jan 2003 17:06:34 -0800


Bugs item #674404, was opened at 2003-01-25 02:13
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=674404&group_id=5470

Category: Python Interpreter Core
Group: Python 2.3
>Status: Closed
>Resolution: Wont Fix
Priority: 5
Submitted By: Viktor Ferenczi (complex)
Assigned to: Nobody/Anonymous (nobody)
Summary: repr of type and class objects

Initial Comment:
I've recently faced a shortcoming of my (our :-)
favourite prog. lang. related to "repr-ing" type
and class objects. Let's define a helper function
to show the problem:

def F(o): return eval(repr(o))==o

Then try this:

>>> F(1)
1
>>> F('x')
1
>>> F(int)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 1, in F
  File "<string>", line 1
    <type 'int'>
    ^
SyntaxError: invalid syntax

>>> str(int)
"<type 'int'>"

>>> repr(int)
"<type 'int'>"

In the case of str(int) returning "<type 'int'>" is ok, since
str() should return human-readable representation.
But repr() should return a python expression can be
evaluated into an object with the same "value" in the
same context, so returning "<type 'int'>" is a mistake.
repr() should return just "int" to satisfy the philosophy
behind repr.

F(int) should return 1, since:

>>> int==int
1

Generally, repr() should return the __name__ attribute if
called with a class object (not an instance of a class).
I had to solve the problem by treating type objects
specially, but it isn't an elegant solution.

I know, that fixing this problem may break some
(badly written) code, but accoring to my view, it's better
to check the type of an object by checking the return
value of type() instead of parsing the output of repr().

In my opinion, this could be fixed easily in version 2.3.

Best regards,
Complex

----------------------------------------------------------------------

>Comment By: Martin v. Löwis (loewis)
Date: 2003-01-26 02:06

Message:
Logged In: YES 
user_id=21627

You are misunderstanding the philosophy of repr. repr 
should, most of all, return an unambiguous representation of 
the object (i.e. one that you can't easily mistake for some 
other object). Only if possible, it should produce something 
that you can pass to eval().

Now, since int.__class__ is type, one would expect that repr
(int) behaves similar to repr(any_other_type). In general, it is 
not possible to know how a certain type object is bound, so 
you cannot change repr for the general case. Changing it for 
the specific case of the types that also happen to be builtins 
is of questionable value.

Unless you can bring a real-world application that can't 
possibly work unless repr is changed, I'm closing this as 
Won't Fix.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=674404&group_id=5470