TAGGER dunder in a class

66 views
Skip to first unread message

Lucas

unread,
May 9, 2026, 7:55:34 AMMay 9
to py4web
hello one and all,

doesn't python automatically know to access the __str__ method of a class when that class is mentioned under a print function?  like:
...
class TNumber0():
def __init__(self, number):
self._number_ = number
def __str__(self):
print('__str__')
return f'{self._number_:.3e}'

n0 = TNumber0(7.77)
print(n0)
...
returns:
__str__
7.770e+00

how can I build into my class a similar dunder, like "__tagger__", like:
...
  def __tagger__(self):
print('__tagger__')
exp = int(floor(log10(abs(self._number_))))
man = self._number_ / 10**exp
return XML(f'{man}x10<sup>{exp}</sup>')
...
that is automatically run when the instance variable is placed within any TAGGER or yatl.helper?

thank you in advance, lucas

Massimo DiPierro

unread,
May 9, 2026, 7:46:15 PMMay 9
to py4web
I think the best way is for your class to extend class TAG and put that logic into an xml(self) method. It should work as you expect. Let us know.

Lucas

unread,
May 10, 2026, 3:32:35 AMMay 10
to py4web
ok,, ,
...
SUP = TAG['sup']

class TNumber0(TAG):

def __init__(self, number):
self._number_ = number
def __str__(self):
print('__str__')
return f'{self._number_:.3e}'
def xml(self):

exp = int(floor(log10(abs(self._number_))))
man = self._number_ / 10**exp
return (man, 'x10', SUP(exp),)
...
returns:
  File "prac0_class.py", line 6, in <module>
    class TNumber0(TAG):
    ...<8 lines>...
    return (man, 'x10', SUP(exp),)
TypeError: __mro_entries__ must return a tuple

in which I tried returning without the surrounding parentheses also, because that would return a tuple also.

Lucas

unread,
May 15, 2026, 4:04:14 AM (10 days ago) May 15
to py4web
hey Massimo,

I worked hard on this because I'm determined for a host of reasons.  I have created a base class like "class TBase(TAGGER)" and all of its descendants of that class have the xml(self) method in which a str is returned and is displayed properly on the browser end to matter what tags are within it.  ok, that works great.

I have one immutable object descendant of float and not TBase above.  I've tried everything I can to render it properly on the browser side, but tags in the str retrieved is always escaped out and shows the tags instead of renders them.

however, is it possible to update under yatl.helpers on line 404 under CAT.xml method to:

        return "".join(
            s.xml() if hasattr(s, 'xml') else xmlescape(unicodeT(s))
            for s in self.children

instead of what is there now:

        return "".join(
            s.xml() if isinstance(s, TAGGER) else xmlescape(unicodeT(s))
            for s in self.children

in the former, it is not escaped and renders properly as long as the xml method is under the object no matter it descend from TAGGER or not.  whereas now, in the latter, it has to descend from TAGGER in order to render properly.

therefore, may you please update the CAT.xml method in the former so that it works better for those none TAGGER descendants.  I really don't want to go through that multi/dual descendant mess.

thank you in advance, lucas

Massimo DiPierro

unread,
May 20, 2026, 6:14:30 PM (4 days ago) May 20
to py4web
This is done

Lucas

unread,
May 20, 2026, 9:59:39 PM (4 days ago) May 20
to py4web
and it works great.  thank you buddy.  have a great day.  
Reply all
Reply to author
Forward
0 new messages