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

SourceForge.net noreply@sourceforge.net
Fri, 24 Jan 2003 17:13:12 -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: Open
Resolution: None
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

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

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