[Tutor] repr()

Alan G alan.gauld at freenet.co.uk
Wed Jun 8 00:29:36 CEST 2005

> Possibly I am missing something, but how do you use the repr()

THere are several ways of using repr, the most common is at the >>>

>>> x = 5
>>> x

when I typed x at the >>> prompt Python called repr(x) to display the
result. This can be slightly different to using print which calls
instead of repr(). Thats why:

>>> s = 'hello'
>>> s
>>> print s

produce different results.

The other waybthat you can use repr() is by using the backtick

>>> print `s`

Note that the quote signs are back, indicating that repr() has been

> def myFunc(): print 'hello'
> Then run
> repr( myFunc )
>Which returns
> '<function myFunc at 0x009C6630>'

Just as you would see if you had done:

>>> def myfunc(): print 'hello'
>>> myfunc
'<function myFunc at 0x009C6630>'

> s = repr( myFunc() )
> print s

Which assigns NOne to s and then prints the repr() of None

> 'None'

As it should.

What did you think it should do?

Alan G.

More information about the Tutor mailing list