[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()
function?
THere are several ways of using repr, the most common is at the >>>
prompt.
>>> x = 5
>>> x
5
>>>
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
str()
instead of repr(). Thats why:
>>> s = 'hello'
>>> s
'hello'
>>> print s
hello
>>>
produce different results.
The other waybthat you can use repr() is by using the backtick
notation
>>> print `s`
'hello'
>>>
Note that the quote signs are back, indicating that repr() has been
used.
> 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