I have the following class, which i want to be represented as a string (only after setting body
value on __init__
):
class Component():
body = ''
template = None
context = {}
def __init__(self):
if self.template:
self.template = 'dashboards/' + self.template
self.body += render_to_string(self.template, context = self.context)
def __repr__(self):
return str(self.body)
So for example, if i print an instance of Title
class down below..
class Title(Component):
template = 'basic_components/title.html'
defaults = {
'text': 'Title',
}
def __init__(self, context=None):
self.context = {**self.defaults, **(context or {})}
super().__init__()
.. i'll get a string object, which is what i wanted/expected due to the parent __repr__
method.
However, i want every Component
to behave like a string, and not only represent as a string.
For example, if i sum two Title
instances, i want the same behavior as summing two str
objects (concat)
I could go over to Component
class and include..
def __add__(self, obj):
return self.__repr__() + obj.__repr__()
..and it would work. However, i want most of the string methods available.
For example, i want to be able to pick a Title
instance and do .join([..])
with it.
I tried inherit str on Component
like so: class Component(str):
but with no sucess.
Also, i've tried to manipulate the __new__
constructor on Component
, but that left me even more confused, since i need self.body
to be calculated before creating the string object, and __new__
is executed before __init__
..
How can i make Component
objects totally behave like str
objects with the value of body
?
EDIT:
class Component(str):
def __new__(cls, body=None, template=None, context=None):
if not body:
body = ''
if template:
template = 'dashboards/' + template
body += render_to_string(template, context = context)
return super().__new__(body)
class Title(Component):
template = 'basic_components/title.html'
defaults = {
'text': 'Title',
}
def __init__(self, context=None):
self.context = {**self.defaults, **(context or {})}
super().__init__()
t = Title()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 8, in __new__
TypeError: str.__new__(X): X is not a type object (str)