[Tutor] Matrix class problems

alan.gauld@bt.com alan.gauld@bt.com
Mon, 1 Jul 2002 00:06:58 +0100


>     Firstly, I would like to know what exactly repr() does. I read the
> docs, and this is what I got :
> ...
> Return the canonical string representation of the object.
> For most object types, eval(repr(object)) == object.
> """
> 
> What does this mean and how is this different from str() ?

repr is a pythonic representation of an object as a string.
str is a human readable version.

You can see the difference at the prompt by typing values 
with and without the print command:

>>> print 5   # uses str()
5
>>> 5         # uses repr()
5
>>>  # the above are identical
>>> s = "Hello world"
>>> print s
Hello world
>>> s
"Hello world"
>>> # notice repr added qutes whereas str didn't

Coming back to the doc string comment consider what 
happens with eval:

eval(Hello world)  # error, looks for variables called hello & world
eval ("Hello world")   # works. It gives us a string back

Hope that makes the diffrence a bt clearer

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld