class Vector(object): def __init__(self, x=0, y=0): self.x = x self.y = y def __repr__(self): return 'Vector(%r, %r)-repr' % (self.x, self.y) def __abs__(self): return 'Vector(%r, %r)-str' % (self.x, self.y)
Result
Comment
The method of
- /repr__() is provided by the object class, and all Python classes are subclasses of the object class, so all Python objects have the method of ___. So if you need to concatenate any object with a string, you can call the method with ___ first to turn the object into a string and then concatenate the strings together.
- repr__() is special because it is a “self-describing” method, usually with the following scenario: when the developer prints the object directly, the system prints out the “self-describing” message with the object, telling the outside world the state the object has . __repr__() the method provided by the
- object always returns the “ class name +object at + memory address ” with the object implementation class. This return value does not really implement the ‘self-describing’ function, so if the user needs to custom class to do it, he/she must rewrite the method with repr__().
- is generally more readable with succinct __str__(), and the return result of each __repr__() is more accurate and more appropriate for the developer. </ li>
- for developers, in the implementation class, suggested rewriting __repr__ () function </ span> </ strong>!
Read More:
- [Solved] Python3.9 Pycropto RSA Error: TypeError: can’t concat str to bytes
- Python 3.7 Error: AttributeError: ‘str‘ object has no attribute ‘decode‘ [How to Solve]
- [Solved] Python Error: TypeError: write() argument must be str, not bytes
- Python PIP TypeError: expected str, bytes or os.PathLike object, not int
- [Solved] python-sutime Error: the JSON object must be str, bytes or bytearray, not ‘java.lang.String‘
- Python TypeError: Unrecognized value type: <class ‘str‘>dateutil.parser._parser.ParserError: Unknow
- Pandas uses str.contains to filter error [How to Solve]
- [Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘
- Djangorestframework-simplejwt: ‘str‘ object has no attribute ‘decode‘ [Solved]
- AttributeError str object has no attribut [How to Solve]
- [resolution] str.contains() problem] valueerror: cannot index with vector containing Na/Nan values
- [Solved] AttributeError: ‘str‘ object has no attribute ‘decode‘
- [Solved] TypeError: unsupported operand type(s) for /: ‘str‘ and ‘int‘
- [Solved] Pytorch-transformers Error: AttributeError: ‘str‘ object has no attribute ‘shape‘
- Windows10 DOTA_devkit Error: TypeError: ‘>=‘ not supported between instances of ‘NoneType‘ and ‘str‘
- Invalid python sd, Fatal Python error: init_fs_encoding: failed to get the Python cod [How to Solve]
- [Solved] NPM install Error: check python checking for Python executable python2 in the PATH
- How to Solve Python WARNING: Ignoring invalid distribution -ip (e:\python\python_dowmload\lib\site-packages)
- [Solved] pycocotools Install Error: ERROR: Error expected str, bytes or os.PathLike object, not NoneType while executing
- [Solved] opencv-python: recipe for target ‘modules/python3/CMakeFiles/opencv_python3.dir/all‘ failed