Getting a dictionary from an object
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Sat Jul 23 23:25:23 EDT 2005
On Sat, 23 Jul 2005 13:50:36 -0400, Mike Meyer wrote:
>> I don't think this is particularly useful behaviour. How do you use it?
>
> def __str__(self):
> return self._format % self
That doesn't work. It calls self.__str__ recursively until Python halts
the process.
>>> class Thing(dict):
... _format = "Thing %s is good."
... def __str__(self):
... return self._format % self
...
>>> X = Thing()
>>> X # calls __repr__ so is safe
{}
>>> str(X) # not safe
File "<stdin>", line 4, in __str__
File "<stdin>", line 4, in __str__
...
File "<stdin>", line 4, in __str__
File "<stdin>", line 4, in __str__
RuntimeError: maximum recursion depth exceeded
--
Steven.
More information about the Python-list
mailing list