[reportlab-users] Many __repr__() methods are really __str__ methods

Stuart Bishop reportlab-users@reportlab.com
Fri, 14 Feb 2003 00:00:59 +1100


On Thursday, February 13, 2003, at 06:31  AM, Norman Shelley wrote:

 > __str(self) methods.   __repr__(self) methods should output a string 
that Python

This is only *convention* in those cases where it is reasonable to do so
(and its only convention - not a guarantees). Plenty of things cannot be
represented in the way you want, or it just wouldn't make sense to do 
so:

 >>> class A: pass
...
 >>> repr(A)
'<class __main__.A at 0x40f600>'
 >>> a = A()
 >>> repr(a)
'<__main__.A instance at 0x4fad50>'
 >>> repr(type(''))
"<type 'str'>"
 >>> repr(repr)
'<built-in function repr>'
 >>>

So your generated script is only working by accident. The way you need 
to
do this is to use pickle module, so your generated script would be made 
up
of lines like:

foo = pickle.loads('<thepickledobject>')

This has the added advantage of throwing an exception when if you try 
and
store something that cannot be reinstantiated rather than wait until you
try and run the output.

-- 
Stuart Bishop <zen@shangri-la.dropbear.id.au>
http://shangri-la.dropbear.id.au/