Raymond Hettinger added the comment:
The UserString1 patch is incorrect as it leads to infinite recursion because
the __rmod__ operation only gets called when the other argument doesn't have
__mod__.
One possible fix is to coerce the template to a regular str:
def __rmod__(self, template):
return self.__class__(str(template) % self)
That would get the following test to pass:
class S:
'strlike class without a __mod__'
def __init__(self, value):
self.value = value
def __str__(self):
return str(self.value)
assert S('say %s') % UserString('hello') == 'say hello'
That said, the goal of UserString is to parallel what a regular string would
do. In this case, a TypeError is raised:
>>> print(S('answer is %s') % 'hello')
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for %: 'S' and 'str'
Serhiy, what do you think should be done, coerce to a str or remove the
__rmod__ method entirely?
----------
assignee: rhettinger -> serhiy.storchaka
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue25652>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com