0
I only changed self._hiddenlist into self when formatting; it'll output a recursion error. What does it actually mean and why?
class Queue: def __init__(self, contents): self._hiddenlist = list(contents) def push(self, value): self._hiddenlist.insert(0, value) def pop(self): return self._hiddenlist.pop(-1) def __repr__(self): return "Queue({})".format(self) queue = Queue([1, 2, 3]) print(queue) queue.push(0) print(queue) queue.pop() print(queue) print(queue._hiddenlist)
7 Respuestas
+ 2
It gives a recursion error because when you use a non-string object as a format parameter, Python will automatically call __repr__ on the object. The thing is, in your __repr__, Python keeps having to call __repr__ to get the string representation until the recursion depth limit is reached.
EDIT: Essentially, the __repr__() definition boils down to:
return "Queue({})".format("Queue({})".format("Queue({})".format("Queue({})"...
+ 7
LunarCoffee you're saying if there's no __str__ magic method added python calls the repr function (which is called when the object as an arg in the print function) to get the string representation. Is that right?
+ 2
In your __repr__() function you put self as the parameter for format(), instead of self._hiddenlist.
+ 1
Yes, I'm aware of that😅
I was changing up stuff to test things out
I was asking: what does it even mean if I am to do this?
+ 1
Awesome. Thanks guys💪
0
So __repr__() can take the place of __str__() if there's not one?
What else can it take the place of?