On Wed, Dec 21, 2022 at 8:54 AM Chris Angelico <ros...@gmail.com> wrote:

> > I think both of those will call self.__str__, which creates a recursion
> -- that's what I'm trying to avoid.
> >
> > I'm sure there are ways to optimize this -- but only worth doing if it's
> worth doing at all :-)
> >
>
> Second one doesn't seem to.
>
> >>> class Str(str):
> ...     def __str__(self):
> ...             print("str!")
> ...             return "spam"
> ...     def __repr__(self):
> ...             print("repr!")
> ...             return "SPAM"
> ...
> >>> s = Str("ham")
> >>> f"{s}"
> str!
> 'spam'
> >>> "".join((s,))
> 'ham'
>

hmm -- interesting trick -- I had jumped to that conclusion -- I wonder
what it IS using under the hood?

Interestingly, neither does the f-string, *if* you include a format
> code with lots of room. I guess str.__format__ doesn't always call
> __str__().
>

Now that you mention that, UserString should perhaps have a __format__,
More evidence that it's not really being maintained.

Though maybe not -- perhaps the inherited one will be fine.

Now that I think about it, perhaps the inherited __str__ would be fine as
well.

-CHB


-- 
Christopher Barker, PhD (Chris)

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UHMZ4PHWU7S3GON43LQKOSOVUI3J6AR4/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to