On Thu, Apr 2, 2020 at 3:27 PM Steven D'Aprano <st...@pearwood.info> wrote:
> Or maybe that's just an argument that no solution is going to solve
> *every* problem. What do we do about people who write this:
>
>     buf = f'{buf}{substring}'
>
> inside a loop? We can't fix everyone's code with one change.
>

I don't know whether your point was that this is bad code and can't be
optimized, or that it's good code but still can't be optimized by this
proposal. But if the former, then I put it to you that this isn't
actually bad code.

text = ""
for thing in stuff:
    # Option 1:
    text += f"{thing.id}: {thing.name} ({thing.cat})\n"
    # Option 2:
    text = f"{text}{thing.id}: {thing.name} ({thing.cat})\n"

Which is going to be (a) faster, and (b) more memory-efficient? What
if you change interpreters? Does it make a difference whether the
amount added per iteration is large or small? What if most of the
content is ASCII but there's one single non-ASCII character somewhere?

It's entirely viable for both forms to exist in the wild, and to be justifiable.

If your argument was that this code is perfectly fine and there's just
too many ways to write good code and we can't hope to optimize them
all, then I apologize, and this post is irrelevant :)

ChrisA
_______________________________________________
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/K5NSOQEZSE6RHIX33KDGKIYQYZCAIFXV/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to