私は Python を急速に学習しており、オブジェクトの表現形式と文字列形式、およびreprメソッドと混同しています。次のコードで x = Point(1, 3) を呼び出して取得します。
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)
!r 変換フィールドが、eval() ステートメントを使用して別の同一のオブジェクトを作成するために Python によって評価できる文字列内の変数を表すためのものである場合、なぜこれが機能しないのですか:
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
!r 仕様は、オブジェクト x 型 Point を Point(1, 3) または最初の実行と同様の表現形式の文字列に作成すると考えました。Python はこの表現 !r を文字列形式でどのように正確に実行し、正確には何を意味するのでしょうか? 2 番目の例が機能しないのはなぜですか?