I've been learning Python rapidly and am getting confused with the representational form and string form of an object as well as the repr method. I call x = Point(1, 3) with the following code and get:
class Point():
def __init__(self, x, y):
'''Initilizae the object'''
self.x = x
self.y = y
def __repr__(self):
return "Point({0.x!r}, {0.y!r})".format(self)
def distance_from_origin(self):
return math.hypot(self.x, self.y)
>>>x
Point(1, 3)
If !r conversion field is for representing the variable in a string that can be evaluated by Python to create another identical object using eval() statement, why doesn't this work:
class Point():
def __init__(self, x, y):
'''Initilizae the object'''
self.x = x
self.y = y
def __repr__(self):
return "{!r}".format(self)
def distance_from_origin(self):
return math.hypot(self.x, self.y)
>>>x
File "C:\...\...\examplepoint.py, line 8, in __repr__
return "{!r}".format(self)
File "C:\...\...\examplepoint.py, line 8, in __repr__
return "{!r}".format(self)
File "C:\...\...\examplepoint.py, line 8, in __repr__
return "{!r}".format(self)
File "C:\...\...\examplepoint.py, line 8, in __repr__
return "{!r}".format(self)
The same error for 100 more lines
RuntimeError: maximum recursion depth exceeded
I thought that the !r specification would create object x type Point into a string in representation form that would look like: Point(1, 3) or similar to the first run. How exactly does Python do this representation !r in string format and what exactly does it mean? Why doesn't the second example work?