Repr() function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Repr() function (/thread-20565.html) |
Repr() function - Valentina - Aug-19-2019 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. Thank you very much for support! Valentina! RE: Repr() function - fishhook - Aug-19-2019 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. RE: Repr() function - Valentina - Aug-22-2019 Thank you very much. RE: Repr() function - perfringo - Aug-22-2019 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) |