Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Repr() function
#1
Hello! I am a Junior in Python. I am doing my first steps.
Please help with repr(function).
I read in Python book that repr() must return "real" meaning.
For example (from book):
>>> num = 1 / 3
>>> repr(num)#
‘0.33333333333333331’
>>> str(num) #
‘0.333333333333’

I tried to use repr() in Sublime Text Editor and in interactive python mode/ But result is same. I receive only 0.333333333333, but never ‘0.33333333333333331’.
I use Python 3.7.2 ver.
[Image: 73fd8a7309c0.jpg]
[Image: 36b89161a1a0.jpg]

Thank you very much for support!
Valentina!
Reply
#2
Quote:Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a __repr__() method.
Reply
#3
Thank you very much.
Reply
#4
fishhook provided comprehensive answer, but you can always start from built-in help and work from that. In many cases built-in help is all you need.

>>> help(repr)
Help on built-in function repr in module builtins:

repr(obj, /)
    Return the canonical string representation of the object.
    
    For many object types, including most builtins, eval(repr(obj)) == obj.
(END)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ascii() (repr() in py2) Skaperen 1 3,900 Jun-05-2017, 10:32 PM
Last Post: Ofnuts
  str vs repr Skaperen 9 5,972 Jun-05-2017, 01:04 AM
Last Post: Skaperen
  when repr() fails Skaperen 8 6,597 May-03-2017, 02:48 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020